I'm using the following Android code to generate a HmacMD5 hash. When
I generate the hash from the Android App I get the following:
J0t��j#߸� bq- �. When I generate the hash from a .net console
application I get: SjB0j+xqI9+4hxticS0Cjw==. Notice the difference
and that the Android has has special chars in it. Is this a character
set issue?
Aaron
==============================================================
Here's the Android code:
public class HMACMD5 {
private final String HMAC_MD5_NAME = "HmacMD5";
private SecretKeySpec sk;
private Mac mac;
public HMACMD5(byte[] key) throws GeneralSecurityException {
init(key);
}
public HMACMD5(String key) throws GeneralSecurityException {
init(EncodingUtils.getAsciiBytes(key));
}
private void init(byte[] key) throws GeneralSecurityException {
sk = new SecretKeySpec(key, HMAC_MD5_NAME);
mac = Mac.getInstance(HMAC_MD5_NAME);
mac.init(sk);
}
public byte[] ComputeHash(byte[] data) {
return mac.doFinal(data);
}
public byte[] ComputeHash(String data) {
return ComputeHash(EncodingUtils.getAsciiBytes(data));
}
}
public String encodeText(String sKey, String sSrc) throws Exception {
HMACMD5 hmacMD5 = new HMACMD5(sKey);
byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
String sEncodedText = EncodingUtils.getString(encodedTextBytes,
"BASE64");
return sEncodedText;
}
==============================================================
Here's the .NET code:
public static string EncodeText(byte[] key, string sText, Encoding
encoding)
{
HMACMD5 hmacMD5 = new HMACMD5(key);
byte[] textBytes = encoding.GetBytes(sText);
byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
string sEncodedText = Convert.ToBase64String(encodedTextBytes);
return sEncodedText;
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en