SHA256不断创建不同的盐值
我编写了SHA256算法的简化版本,并尝试了一些输入。首先,它似乎可以正常工作(尽管输出与在线代码有所不同),但是当我尝试使用Input “ Admin”
尝试时,它会输出相同的哈希,无论添加到输入中的盐字符串如何。出乎意料的是,如果输入为“ admi
”,“ admin1”
或“ admin1234”
,它仍然会输出相同的哈希。我找不到其他有相同问题的输入。 这是代码:
package src.encrypt;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Random;
public class Password {
private String salt;
private String hash;
private String saltGen() {
final String hex = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random rnd = new Random(Instant.now().toEpochMilli());
char[] salt = new char[16];
for (int i = 0; i < 16; i++) {
int n = rnd.nextInt(hex.length());
salt[i] = hex.charAt(n);
}
return String.valueOf(salt);
}
public Password() {
};
public Password(String pass) {
salt = saltGen();
String p = new String(pass + salt);
hash = sha256(p);
}
public String getSalt() {
return salt;
}
public String getHash() {
return hash;
}
public boolean compareHash(String h) {
return getHash().equals(sha256(h + getSalt()));
}
// SHA-256 Hashing Algorithm
public String sha256(String str) {
// Pre-processing
ArrayList<String> hash = new ArrayList<>();
byte[] bytes = str.getBytes();
int i = 0;
for (i = 0; i < bytes.length - 4; i += 4) {
String tmp = String.format("%8s", Integer.toBinaryString(bytes[i] & 0xff)).replace(' ', '0')
+ String.format("%8s", Integer.toBinaryString(bytes[i + 1] & 0xff)).replace(' ', '0') +
String.format("%8s", Integer.toBinaryString(bytes[i + 2] & 0xff)).replace(' ', '0') +
String.format("%8s", Integer.toBinaryString(bytes[i + 3] & 0xff)).replace(' ', '0');
hash.add(tmp);
}
String temp = new String();
for (int j = i; j < bytes.length; j++)
temp = new String(temp + String.format("%8s", Integer.toBinaryString(bytes[j] & 0xff)).replace(' ', '0'));
if (temp.length() < 32)
temp = new String(temp + "10000000");
while (temp.length() < 32)
temp = new String(temp + "00000000");
hash.add(temp);
int l = 14 - hash.size() % 16;
for (i = 0; i < l; i++)
hash.add(new String("00000000000000000000000000000000"));
String tmp = String.format("%64s", Long.toBinaryString((long) (bytes.length * 8))).replace(' ', '0');
hash.add(tmp.substring(0, 32));
hash.add(tmp.substring(32));
// Init Hash values
int H[] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};
// Init Round Constants
int K[] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
// Create Message Schedule and Compressing
l = hash.size() / 16;
int a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7];
ArrayList<Integer> w = new ArrayList<>();
for (i = 0; i < l; i++) {
for (int j = i * 16; j < (i + 1) * 16; j++) {
w.add(Integer.parseInt(hash.get(j), 2));
}
for (int j = 16; j < 64; j++) {
int s0 = rightRotate1(w.get(j - 15));
int s1 = rightRotate2(w.get(j - 15));
w.add(w.get(j - 16) + s0 + w.get(j - 7) + s1);
}
for (int j = 0; j < 64; j++) {
int temp1 = h + rightRotate3(e) + ((e & f) ^ (~e & g)) + K[i] + w.get(i);
int temp2 = rightRotate4(a) + ((a & b) ^ (a & c) ^ (b & c));
h = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
// Modify final values
H[0] = H[0] + a;
H[1] = H[1] + b;
H[2] = H[2] + c;
H[3] = H[3] + d;
H[4] = H[4] + e;
H[5] = H[5] + f;
H[6] = H[6] + g;
H[7] = H[7] + h;
}
// Concatenate final hash
String s = new String();
for (i = 0; i < 8; i++) {
s = new String(s + String.format("%08X", H[i]).toLowerCase());
}
return s;
};
// right rotate
private int rotr(int x, int n) {
return (x >>> n) | (x << (32 - n));
}
private int rightRotate3(int x) {
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
}
private int rightRotate4(int x) {
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
}
private int rightRotate1(int x) {
return rotr(x, 7) ^ rotr(x, 18) ^ x >>> 3;
}
private int rightRotate2(int x) {
return rotr(x, 17) ^ rotr(x, 19) ^ x >>> 10;
}
使用输入“ admin”
,输出始终为“ 0920B401857E70A28C7B645C04195F2F2F2F15B54B98A7E78A7E7810F6D46D46D46C3A9C79C74ADF6A” 谁能发现我犯的一些错误或错误?
请注意,我是Java的新手,所以我使用的某些方法可能不效率。
编辑 这是我称呼该方法的方式:
// Main.java
import src.encrypt.Password;
public class Main {
public static void main(String[] args) {
String pass = "admin";
Password p = new Password(pass);
System.out.println(p.getSalt()); // different calls, different values
System.out.println(p.getHash()); // different calls, same value
}
}
I wrote a simplified version of SHA256 algorithm and tried some inputs. At first it seems to work fine(although the output is a bit different from the online codes), but when I tried with input "admin"
, it outputs the same hash regardless of salt String added to input. Surprisingly, if input is "admi
", "admin1"
or "admin1234"
, it still outputs the same hash. I didn't find any other input that has the same problem.
Here's the code:
package src.encrypt;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Random;
public class Password {
private String salt;
private String hash;
private String saltGen() {
final String hex = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random rnd = new Random(Instant.now().toEpochMilli());
char[] salt = new char[16];
for (int i = 0; i < 16; i++) {
int n = rnd.nextInt(hex.length());
salt[i] = hex.charAt(n);
}
return String.valueOf(salt);
}
public Password() {
};
public Password(String pass) {
salt = saltGen();
String p = new String(pass + salt);
hash = sha256(p);
}
public String getSalt() {
return salt;
}
public String getHash() {
return hash;
}
public boolean compareHash(String h) {
return getHash().equals(sha256(h + getSalt()));
}
// SHA-256 Hashing Algorithm
public String sha256(String str) {
// Pre-processing
ArrayList<String> hash = new ArrayList<>();
byte[] bytes = str.getBytes();
int i = 0;
for (i = 0; i < bytes.length - 4; i += 4) {
String tmp = String.format("%8s", Integer.toBinaryString(bytes[i] & 0xff)).replace(' ', '0')
+ String.format("%8s", Integer.toBinaryString(bytes[i + 1] & 0xff)).replace(' ', '0') +
String.format("%8s", Integer.toBinaryString(bytes[i + 2] & 0xff)).replace(' ', '0') +
String.format("%8s", Integer.toBinaryString(bytes[i + 3] & 0xff)).replace(' ', '0');
hash.add(tmp);
}
String temp = new String();
for (int j = i; j < bytes.length; j++)
temp = new String(temp + String.format("%8s", Integer.toBinaryString(bytes[j] & 0xff)).replace(' ', '0'));
if (temp.length() < 32)
temp = new String(temp + "10000000");
while (temp.length() < 32)
temp = new String(temp + "00000000");
hash.add(temp);
int l = 14 - hash.size() % 16;
for (i = 0; i < l; i++)
hash.add(new String("00000000000000000000000000000000"));
String tmp = String.format("%64s", Long.toBinaryString((long) (bytes.length * 8))).replace(' ', '0');
hash.add(tmp.substring(0, 32));
hash.add(tmp.substring(32));
// Init Hash values
int H[] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};
// Init Round Constants
int K[] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
// Create Message Schedule and Compressing
l = hash.size() / 16;
int a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7];
ArrayList<Integer> w = new ArrayList<>();
for (i = 0; i < l; i++) {
for (int j = i * 16; j < (i + 1) * 16; j++) {
w.add(Integer.parseInt(hash.get(j), 2));
}
for (int j = 16; j < 64; j++) {
int s0 = rightRotate1(w.get(j - 15));
int s1 = rightRotate2(w.get(j - 15));
w.add(w.get(j - 16) + s0 + w.get(j - 7) + s1);
}
for (int j = 0; j < 64; j++) {
int temp1 = h + rightRotate3(e) + ((e & f) ^ (~e & g)) + K[i] + w.get(i);
int temp2 = rightRotate4(a) + ((a & b) ^ (a & c) ^ (b & c));
h = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
// Modify final values
H[0] = H[0] + a;
H[1] = H[1] + b;
H[2] = H[2] + c;
H[3] = H[3] + d;
H[4] = H[4] + e;
H[5] = H[5] + f;
H[6] = H[6] + g;
H[7] = H[7] + h;
}
// Concatenate final hash
String s = new String();
for (i = 0; i < 8; i++) {
s = new String(s + String.format("%08X", H[i]).toLowerCase());
}
return s;
};
// right rotate
private int rotr(int x, int n) {
return (x >>> n) | (x << (32 - n));
}
private int rightRotate3(int x) {
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
}
private int rightRotate4(int x) {
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
}
private int rightRotate1(int x) {
return rotr(x, 7) ^ rotr(x, 18) ^ x >>> 3;
}
private int rightRotate2(int x) {
return rotr(x, 17) ^ rotr(x, 19) ^ x >>> 10;
}
With input "admin"
, output is always "0920b401857e70a28c7b645c04195f2f15b54b98a7e7810f6d46c3a9c74adf6a"
Can anyone spot some bugs or mistakes that I made?
Please note that I'm new to Java so some methods I use maybe unefficient.
EDIT
Here's how I call the method:
// Main.java
import src.encrypt.Password;
public class Main {
public static void main(String[] args) {
String pass = "admin";
Password p = new Password(pass);
System.out.println(p.getSalt()); // different calls, different values
System.out.println(p.getHash()); // different calls, same value
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误发生在消息时间表数组的处理中(
w
arrayList,术语来自 https://en.wikipedia.org/wiki/sha-2 ):您的循环变量是
j
,但是您使用变量i
i < /代码>访问消息时间表阵列(
i
在处理消息块的外循环中使用)。正确的代码是
The error occurs in the processing of the message schedule array (the
w
arraylist, the terminology is from https://en.wikipedia.org/wiki/SHA-2):Your loop variable here is
j
, but you use the variablei
to access the message schedule array (i
is used in the outer loop that processes the message chunks).The correct code is