Created
January 15, 2015 04:17
-
-
Save firoze/d732dab55b89d0b96ed9 to your computer and use it in GitHub Desktop.
SHA1 Some String in Java
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
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
public class HashTextTest { | |
/** | |
* @param args | |
* @throws NoSuchAlgorithmException | |
*/ | |
public static void main(String[] args) throws NoSuchAlgorithmException { | |
System.out.println(sha1("test string to sha1")); | |
} | |
static String sha1(String input) throws NoSuchAlgorithmException { | |
MessageDigest mDigest = MessageDigest.getInstance("SHA1"); | |
byte[] result = mDigest.digest(input.getBytes()); | |
StringBuffer sb = new StringBuffer(); | |
for (int i = 0; i < result.length; i++) { | |
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1)); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment