Skip to content

Instantly share code, notes, and snippets.

@chris-piekarski
Last active December 24, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-piekarski/6816085 to your computer and use it in GitHub Desktop.
Save chris-piekarski/6816085 to your computer and use it in GitHub Desktop.
Hash a String using SHA-1 and return hash as String
private static String getSha1Hash(String value) {
MessageDigest digester = null;
String sha1Value = null;
try {
digester = MessageDigest.getInstance("SHA-1");
digester.update(value.getBytes("UTF-8"), 0, value.length());
byte[] sha1hash = digester.digest();
StringBuilder sb = new StringBuilder();
for( byte b : sha1hash )
{
sb.append( String.format("%02X", b) );
}
sha1Value = sb.toString();
}
catch (Exception e) {
Log.e(TAG, "Exception: "+e);
throw new RuntimeException(e);
}
return sha1Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment