Created
September 18, 2013 10:02
-
-
Save xianminx/6607054 to your computer and use it in GitHub Desktop.
Android HMAC-SHA1 Different than Standard Java HMAC-SHA1 http://stackoverflow.com/questions/7047487/android-hmac-sha1-different-than-standard-java-hmac-sha1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String base_string = "This is a test string"; | |
String key = "testKey"; | |
try { | |
Mac mac = Mac.getInstance("HmacSHA1"); | |
SecretKeySpec secret = new SecretKeySpec(key.getBytes("UTF-8"), mac.getAlgorithm()); | |
mac.init(secret); | |
byte[] digest = mac.doFinal(base_string.getBytes()); | |
String enc = new String(digest); | |
// Base 64 Encode the results | |
String retVal = Base64.encodeBase64String(enc.getBytes()); | |
Log.v(TAG, "String: " + base_string); | |
Log.v(TAG, "key: " + key); | |
Log.v(TAG, "result: " + retVal); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment