SHA-512, SHA-512/224, SHA-512/256¶
SHA-512 and its two truncated variants (SHA-512/224 and SHA-512/256) belong to the SHA-2_ family of cryptographic hashes. The three functions produce the digest of a message, respectively 512, 224 or 256 bits long.
SHA-512 is roughly 50% faster than SHA-224 and SHA-256 on 64-bit machines, even if its digest is longer. The speed-up is due to the internal computation being performed with 64-bit words, whereas the other two hash functions employ 32-bit words.
SHA-512/224, SHA-512/256, and SHA-384 too are faster on 64-bit machines for the same reason.
This is an example showing how to use SHA-512:
>>> from Crypto.Hash import SHA512
>>>
>>> h = SHA512.new()
>>> h.update(b'Hello')
>>> print(h.hexdigest())
3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315
This is an example showing how to use SHA-512/256:
>>> from Crypto.Hash import SHA512
>>>
>>> h = SHA512.new(truncate="256")
>>> h.update(b'Hello')
>>> print(h.hexdigest())
7e75b18b88d2cb8be95b05ec611e54e2460408a2dcf858f945686446c9d07aac
SHA stands for Secure Hash Algorithm.
Warning
SHA-512 is vulnerable to length-extension attacks, which are relevant if you are computing the hash of a secret message.
For instance, let’s say you were planning to build a cheap MAC by concatenating a secret key to a public message m (bad idea!):
By only knowing the digest h and the length of m and k, the attacker can easily compute a second digest h’:
where p is a well-known bit string and the attacker can pick a bit string z at will.
The two variants SHA-512/224 and SHA-512/256 are not vulnerable to length-extension attacks.
SHA-512 cryptographic hash algorithm.
SHA-512 belongs to the SHA-2_ family of cryptographic hashes. It produces the 512 bit digest of a message.
>>> from Crypto.Hash import SHA512
>>>
>>> h = SHA512.new()
>>> h.update(b'Hello')
>>> print h.hexdigest()
SHA stands for Secure Hash Algorithm.
- class Crypto.Hash.SHA512.SHA512Hash(data=None)¶
Class that implements a SHA-512 hash
- Undocumented:
block_size
- new(data=None)¶
Return a fresh instance of the hash object.
Unlike the copy method, the internal state of the object is empty.
- Parameters:
- databyte string
The next chunk of the message being hashed.
- Return:
A hash object of the same type
- oid = b'\x06\t`\x86H\x01e\x03\x04\x02\x03'¶
ASN.1 Object identifier (OID):
id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3) nistalgorithm(4) hashalgs(2) 3 }
This value uniquely identifies the SHA-512 algorithm.
- Crypto.Hash.SHA512.digest_size = 64¶
The size of the resulting hash in bytes.
- Crypto.Hash.SHA512.new(data=None)¶
Return a fresh instance of the hash object.
- Parameters:
- databyte string
The very first chunk of the message to hash. It is equivalent to an early call to SHA512Hash.update(). Optional.
- Return:
A SHA512Hash object