Created
August 28, 2013 14:54
-
-
Save dleast/6366943 to your computer and use it in GitHub Desktop.
SHA1 Hash fuction - JAVA
This file contains hidden or 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
public class Sha1Hash { | |
/** Creates a new instance of Sha1Hash */ | |
public Sha1Hash() { | |
} | |
/** | |
* @param inputData | |
* @return | |
*/ | |
public String getHashedValue(String inputData) { | |
String sResp = null; | |
try { | |
byte byteHash[]; | |
MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); | |
sha1.update(inputData.getBytes("utf-8")); | |
byteHash = sha1.digest(); | |
sha1.reset(); | |
// see code snippet below.. | |
String hash = Base64Util.encode(byteHash); | |
return hash.substring(0, (hash.length()/2)); | |
} catch (Exception e) { | |
System.err.println("getHashedValue failed: " + e.getMessage()); | |
return null; | |
} | |
} | |
private String toHexString(byte[] array) { | |
StringBuilder sb = new StringBuilder (); | |
for (int i=0; i< array.length; i++) { | |
String hex = "0"+Integer.toHexString(array[i]); | |
String end = hex.substring(hex.length()-2); | |
sb.append(end.toUpperCase()); | |
} | |
System.err.println("HASH:"+sb.toString()); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment