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.)
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.
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.
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.
来源:https://stackoverflow.com/questions/3407554/how-to-set-messagedigest-seed