I was generating HashValue for contents in the file. I want a different hash value every time I wanna generate even if the file is the same. I am using SHA256. For the same files, the hash value I am getting is the same. Is there any way to generate different hash values?

  • 2
    $\begingroup$ What's your goal? If the hash isn't consistent, how is it different than just generating random bits strings independently of the file? $\endgroup$
    – Maeher
    Apr 28, 2021 at 4:54
  • 2
    $\begingroup$ Append the timedate in millis and then hash it $\endgroup$ Apr 28, 2021 at 6:14
  • 1
    $\begingroup$ HMAC with different keys? $\endgroup$
    – kelalaka
    Apr 28, 2021 at 13:42

1 Answer 1


You should use a cryptographic salt.

This is a single use number generated for each hash. The salt should be prepended to the file to be hashed before hashing. The salt and hash value can then be stored together. Now if you want to compare a file against a given hash you prepend the salt, hash and check against the stored value.

Provided that the salt values are unique, you should not get repeated values (subject to the birthday bound).

  • $\begingroup$ You can also use an HMAC, or a hash algorithm with a personalization parameter (e.g. BLAKE2). $\endgroup$
    – forest
    Apr 30, 2021 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.