Skip to content

Instantly share code, notes, and snippets.

@firoze
Created January 15, 2015 04:17
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 firoze/d732dab55b89d0b96ed9 to your computer and use it in GitHub Desktop.
Save firoze/d732dab55b89d0b96ed9 to your computer and use it in GitHub Desktop.
SHA1 Some String in Java
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