| 1 |
SHA-512, SHA-512/224, SHA-512/256 |
| 2 |
================================= |
| 3 |
|
| 4 |
SHA-512 and its two truncated variants (SHA-512/224 and SHA-512/256) belong to the SHA-2_ family of cryptographic hashes. |
| 5 |
The three functions produce the digest of a message, respectively 512, 224 or 256 bits long. |
| 6 |
|
| 7 |
SHA-512 is roughly 50% faster than SHA-224 and SHA-256 on 64-bit machines, even if its digest is longer. |
| 8 |
The speed-up is due to the internal computation being performed with 64-bit words, |
| 9 |
whereas the other two hash functions employ 32-bit words. |
| 10 |
|
| 11 |
SHA-512/224, SHA-512/256, and SHA-384 too are faster on 64-bit machines for the same reason. |
| 12 |
|
| 13 |
This is an example showing how to use SHA-512: |
| 14 |
|
| 15 |
>>> from Crypto.Hash import SHA512 |
| 16 |
>>> |
| 17 |
>>> h = SHA512.new() |
| 18 |
>>> h.update(b'Hello') |
| 19 |
>>> print(h.hexdigest()) |
| 20 |
3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315 |
| 21 |
|
| 22 |
This is an example showing how to use SHA-512/256: |
| 23 |
|
| 24 |
>>> from Crypto.Hash import SHA512 |
| 25 |
>>> |
| 26 |
>>> h = SHA512.new(truncate="256") |
| 27 |
>>> h.update(b'Hello') |
| 28 |
>>> print(h.hexdigest()) |
| 29 |
7e75b18b88d2cb8be95b05ec611e54e2460408a2dcf858f945686446c9d07aac |
| 30 |
|
| 31 |
|
| 32 |
*SHA* stands for Secure Hash Algorithm. |
| 33 |
|
| 34 |
.. warning:: |
| 35 |
SHA-512 is vulnerable to `length-extension attacks <https://crypto.stackexchange.com/questions/3978/understanding-the-length-extension-attack>`_, |
| 36 |
which are relevant if you are computing the hash of a secret message. |
| 37 |
|
| 38 |
For instance, let's say you were planning to build a cheap MAC by concatenating a secret *key* to |
| 39 |
a public message *m* (bad idea!): |
| 40 |
|
| 41 |
.. math:: |
| 42 |
h = \text{SHA-512}(m || k) |
| 43 |
|
| 44 |
By only knowing the digest *h* and the length of *m* and *k*, the attacker can easily compute a second digest *h'*: |
| 45 |
|
| 46 |
.. math:: |
| 47 |
h' = \text{SHA-512}(m || p || z) |
| 48 |
|
| 49 |
where *p* is a well-known bit string and the attacker can pick a bit string *z* at will. |
| 50 |
|
| 51 |
The two variants SHA-512/224 and SHA-512/256 are **not** vulnerable to |
| 52 |
length-extension attacks. |
| 53 |
|
| 54 |
.. _SHA-2: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf |
| 55 |
|
| 56 |
.. automodule:: Crypto.Hash.SHA512 |
| 57 |
:members: |