-2
a = hashlib.sha512("data").hexdigest()[:32]

What is signifance of using hexdigest here ?

cryptoKTM
  • 2,593
  • 22
  • 21
  • 2
    I'm voting to close this question as off-topic because it's a cross-site duplicate. – tripleee Apr 07 '18 at 12:07
  • 2
    Now that you edited your question to mean something else, the provided answer doesn't make much sense anymore. Where is the reference to sha512-half? – Artjom B. Apr 07 '18 at 12:29

1 Answers1

2

Hash halving (SHA256-half, SHA512-half...) is generating a hash and truncating it to half its size, thus SHA512-half becomes equivalent to SHA256. While a larger hash function accepts larger input, once truncated it has the same collision rate as a non-truncated hash of the said size, and if the hashes we're talking about are solid and vetted (like both SHA256 and SHA512), halving a larger hash to fit the size of the smaller one can only add to perceived security but technically is no different than just generating the smaller hash.

Anyway, if you really don't want to just generate a SHA256, you can halve a SHA512 hash as:

halved_sha512 = hashlib.sha512(b"input").digest()[:32]

For bytes, or:

halved_sha512 = hashlib.sha512(b"input").hexdigest()[:64]

For a hex string.

zwer
  • 24,943
  • 3
  • 48
  • 66