a = hashlib.sha512("data").hexdigest()[:32]
What is signifance of using hexdigest here ?
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.