java - SHA-1 的实现产生的散列值与 `java.security` 实现不同
问题描述
我正在尝试在 Java 11 中实现 SHA-1 算法,并且在测试散列算法时,我得到的散列值与使用java.security
SHA-1 实现散列时不同。
我试图遵循的伪代码可以在 Wikipedia上找到。
public static byte[] hash(byte[] message) {
int h0 = 0x67452301;
int h1 = 0xEFCDAB89;
int h2 = 0x98BADCFE;
int h3 = 0x10325476;
int h4 = 0xC3D2E1F0;
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.writeBytes(message);
out.write(0x00000080);
while (out.size() % 64 != 56) out.write(0x00000000);
out.writeBytes(ByteBuffer.allocate(8).putLong(message.length).array());
byte[] data = out.toByteArray();
for (int j = 0; j < data.length / 64; ++j) {
int[] w = new int[80];
for (int i = 0; i < 16; ++i) {
w[i] = ByteBuffer.wrap(data, j * 64 + i * 4, 4).getInt();
}
for (int i = 16; i < 80; ++i) {
w[i] = leftrotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1);
}
int a = h0;
int b = h1;
int c = h2;
int d = h3;
int e = h4;
for (int i = 0; i < 80; ++i) {
final int f, k;
if (i < 20) {
f = (b & c) | ((~b) & d);
k = 0x5A827999;
} else if (i < 40) {
f = b ^ c ^ d;
k = 0x6ED9EBA1;
} else if (i < 60) {
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
} else {
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
int t = leftrotate(a, 5) + f + e + k + w[i];
e = d;
d = c;
c = leftrotate(b, 30);
b = a;
a = t;
}
h0 += a;
h1 += b;
h2 += c;
h3 += d;
h4 += e;
}
ByteBuffer buffer = ByteBuffer.allocate(20);
buffer.putInt(h0);
buffer.putInt(h1);
buffer.putInt(h2);
buffer.putInt(h3);
buffer.putInt(h4);
return buffer.array();
}
public static int leftrotate(int x, int c) {
return (x << c) | (x >> (32 - c));
}
为了测试这一点,我尝试散列一个 n 字节的随机数组,并将散列与通过
MessageDigest.getInstance("SHA-1").digest(message)
我得到不同的哈希值。我上面的实现有什么错误吗?错误可能来自其他地方吗?
解决方案
实施有两个问题。首先,我以字节而不是位为单位写入初始消息的大小。其次,该leftrotate
方法在应该使用逻辑右移时使用了算术右移。
推荐阅读
- python - 如何使用 daframe 中的数据从 python 更新表
- javascript - 如何在 NuxtAuth v.4 中获取令牌值?
- firebase - 在 Kotlin 中处理收集的正确方法是什么?
- python-3.x - 小部件不可用。请安装 widgetsnbextension 或 ipywidgets 4.0
- java - 我的应用程序无法在 android 6 上运行,但如果在 android 9-11 上运行则可以运行
- bash - bash while 循环给出语法错误,说缺少'['
- android - 如何使用适用于 Android 和 iOS 的 Xamarin 表单在特定频道上的 youtube 上上传视频?
- javascript - 部署到 github 页面时不显示图像
- c# - MassTransit - 未消耗随机中介响应
- javascript - 单击按钮时如何删除特定元素