Skip to content

Instantly share code, notes, and snippets.



Created Jan 15, 2015
What would you like to do?
SHA1 Some String in Java
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