Skip to content

Instantly share code, notes, and snippets.

@nibocn
Created October 29, 2015 03:18
Show Gist options
  • Save nibocn/d01a6d8b66a85aa067ff to your computer and use it in GitHub Desktop.
Save nibocn/d01a6d8b66a85aa067ff to your computer and use it in GitHub Desktop.
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