AuthMe的SHA256加密方法怎样在JAVA里复现
或者说如果想让Java插入语句来注册应该怎样做
加密前:cookie265265
AuthMe:$SHA$0f88639171a6fab3$5597c555066d3b5f07b510a9b3f943999734c440db553c9c51a25588667ad200
在线加密:795c1f2974c372b70b1e26eea077a65d89b7ee852a7f882115074d16e05fe51c
完全对不上呀
或者说如果想让Java插入语句来注册应该怎样做
加密前:cookie265265
AuthMe:$SHA$0f88639171a6fab3$5597c555066d3b5f07b510a9b3f943999734c440db553c9c51a25588667ad200
在线加密:795c1f2974c372b70b1e26eea077a65d89b7ee852a7f882115074d16e05fe51c
完全对不上呀
本帖最后由 Molean 于 2021-6-29 14:05 编辑
$SHA$0f88639171a6fab3$5597c555066d3b5f07b510a9b3f943999734c440db553c9c51a25588667ad200
红色的是加密方式,橘色的是salt俗称盐,白色的是密文。
加密代码如下:
public static String sha256(String message) {
String hash = null;
try {
MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
algorithm.reset();
algorithm.update(message.getBytes());
byte[] digest = algorithm.digest();
hash = String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
return hash;
}
public static String getSaltedSha256(String password, String salt) {
return "$SHA$" + salt + "$" + sha256(sha256(password) + salt);
}
public static void main(String[] args) {
System.out.println(getSaltedSha256("cookie265265", "0f88639171a6fab3"));
}
补充:
盐用于防撞库,简单来说:对于单向加密函数f, 有f(明文,盐)=密文
你只需获取盐, 然后直接调我写的getSaltedSha256(passwd,salt)与密文判等即可.
$SHA$0f88639171a6fab3$5597c555066d3b5f07b510a9b3f943999734c440db553c9c51a25588667ad200
红色的是加密方式,橘色的是salt俗称盐,白色的是密文。
加密代码如下:
public static String sha256(String message) {
String hash = null;
try {
MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
algorithm.reset();
algorithm.update(message.getBytes());
byte[] digest = algorithm.digest();
hash = String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
return hash;
}
public static String getSaltedSha256(String password, String salt) {
return "$SHA$" + salt + "$" + sha256(sha256(password) + salt);
}
public static void main(String[] args) {
System.out.println(getSaltedSha256("cookie265265", "0f88639171a6fab3"));
}
补充:
盐用于防撞库,简单来说:对于单向加密函数f, 有f(明文,盐)=密文
你只需获取盐, 然后直接调我写的getSaltedSha256(passwd,salt)与密文判等即可.