Skip to content

Instantly share code, notes, and snippets.

@xianminx
Created September 18, 2013 10:02
Show Gist options
  • Save xianminx/6607054 to your computer and use it in GitHub Desktop.
Save xianminx/6607054 to your computer and use it in GitHub Desktop.
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