Created
February 28, 2012 03:15
-
-
Save joelmartinez/1929080 to your computer and use it in GitHub Desktop.
hashing a string in android
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
/** pieced together from http://www.androidsnippets.com/create-a-md5-hash-and-dump-as-a-hex-string */ | |
public String md5(String s) { | |
try { | |
// Create MD5 Hash | |
MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); | |
digest.update(s.getBytes()); | |
byte messageDigest[] = digest.digest(); | |
// Create Hex String | |
StringBuffer hexString = new StringBuffer(); | |
for (int i = 0; i < messageDigest.length; i++) { | |
String h = Integer.toHexString(0xFF & messageDigest[i]); | |
while (h.length() < 2) | |
h = "0" + h; | |
hexString.append(h); | |
} | |
return hexString.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment