如何设置MessageDigest种子?
MessageDigest 类实现 SHA-1 算法(以及许多其他算法)。 SHA-1 算法允许使用不同的“种子”或初始摘要。请参阅 SHA-1 Psuedocode
算法初始化变量或种子:
Initialize variables:
h0 = 0x67452301
h1 = 0xEFCDAB89
h2 = 0x98BADCFE
h3 = 0x10325476
h4 = 0xC3D2E1F0
但是 MessageDigest 类,如 在线Java手册,没有提供用于设置这些初始变量的API。事实上,它没有说明初始变量的值。
如何设置 SHA-1 算法的初始种子?
哪里有 Java 中 SHA-1 的示例,使用初始种子?
(我正在寻找 SHA-1 实现,除非示例使用带有替代初始种子的 MessageDigest
。)
The MessageDigest class implements the SHA-1 algorithm (among many others). The SHA-1 algorithm allows one to use different "seeds" or initial digests. See SHA-1 Psuedocode
The algorithm initializes variables, or the seed:
Initialize variables:
h0 = 0x67452301
h1 = 0xEFCDAB89
h2 = 0x98BADCFE
h3 = 0x10325476
h4 = 0xC3D2E1F0
However the MessageDigest class, as described in the Online Java Manual, provides no API for setting these initial variables. In fact, it doesn't state the value of the initial variables.
How can I set the initial seed for the SHA-1 algorithm?
Where is an example of SHA-1 in Java, USING AN INITIAL SEED?
(I'm looking for the SHA-1 implementation, unless the example uses MessageDigest
with an alternative initial seed.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无法为 Java 函数提供初始种子。
我复制了 SHA-1 算法的 C 实现并对其进行了修改以允许更改初始种子值。
The Java function cannot be supplied with an initial seed.
I copied a C implementation of SHA-1 algorithm and modified it to allow changing of the initial seed values.
您认为 SHA-1 摘要中需要种子吗?通常,在需要随机数源的加密算法中,“需要”种子。但在 SHA-1 中,您甚至根本不使用随机数,因此无需设置种子或初始向量。您提到的变量是“硬”变量(常量),它们是算法的一部分,不需要或用于更改 h0-4 的值。
Where do you see the need for a seed in a SHA-1 digest? Normally in encryption algorithm with a need of source of random numbers, a seed is "needed". But in SHA-1 you don't even use random numbers at all, so there is no seed or initial vector to set. The variables you mentioned are 'hard' (constants), they are part of the algorithm, no need or use to change the values of h0-4.
我建议对 MessageDigest 系列哈希函数使用盐而不是种子。例如,通过将盐字节添加到输入来应用盐。
预先添加盐也比直接设置种子值更强大,因为除了更改哈希的内部状态之外,如果盐不是摘要块大小的倍数,那么它还会扰乱输入的对齐方式输入到哈希函数中。
I recommend using a salt instead of a seed for MessageDigest family hash functions. A salt is applied by, for example, prepending the salt bytes to the input.
Prepending a salt is also more powerful than directly setting the seed values, because in addition to changing the internal state of the hash, if the salt is not a multiple of the digest block size then it can also perturb the alignment with which the input is fed into the hash function.