Created
October 29, 2015 03:18
-
-
Save nibocn/d01a6d8b66a85aa067ff 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
public class SHA256 { | |
private static byte[] getHash(String password) { | |
MessageDigest digest; | |
try { | |
digest = MessageDigest.getInstance("SHA-256"); | |
digest.reset(); | |
return digest.digest(password.getBytes()); | |
} catch (NoSuchAlgorithmException e1) { | |
e1.printStackTrace(); | |
} | |
return new byte[]{}; | |
} | |
public static void main(String[] args) { | |
String str = "sdfsdfdsf"; | |
System.out.println("加密前的数据:" + str); | |
byte[] data = getHash(str); | |
String s = String.format("%0" + (data.length * 2) + "X", | |
new BigInteger(1, data)).toLowerCase(); | |
System.out.println("加密后的数据:" + s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment