Last active
December 19, 2015 22:59
-
-
Save regispires/6031450 to your computer and use it in GitHub Desktop.
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 Digest { | |
public static String md5(String str) { | |
return digest(str, "MD5"); | |
} | |
public static String sha(String str) { | |
return digest(str, "SHA"); | |
} | |
public static String sha512(String str) { | |
return digest(str, "SHA-512"); | |
} | |
public static String digest(String input, String algorithm) { | |
return bytesToString(digest(input.getBytes(), algorithm)); | |
} | |
public static byte[] digest(byte[] input, String algorithm) { | |
try { | |
MessageDigest digest = MessageDigest.getInstance(algorithm); | |
digest.update(input); | |
return digest.digest(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static String bytesToString(byte[] bytes) { | |
StringBuffer sb = new StringBuffer(); | |
for (int i = 0; i < bytes.length; i++) { | |
String str = Integer.toHexString(bytes[i] & 0xFF); | |
if (str.length() == 1) | |
sb.append('0'); | |
sb.append(str); | |
} | |
return sb.toString(); | |
} | |
public static void main(String[] args) { | |
System.out.println(Digest.sha512("senha_do_usuario")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment