| 1 |
SHA-256 |
| 2 |
======= |
| 3 |
|
| 4 |
SHA-256 belongs to the SHA-2_ family of cryptographic hashes. |
| 5 |
It produces the 256 bit digest of a message. |
| 6 |
|
| 7 |
>>> from Crypto.Hash import SHA256 |
| 8 |
>>> |
| 9 |
>>> h = SHA256.new() |
| 10 |
>>> h.update(b'Hello') |
| 11 |
>>> print h.hexdigest() |
| 12 |
|
| 13 |
*SHA* stands for Secure Hash Algorithm. |
| 14 |
|
| 15 |
.. warning:: |
| 16 |
SHA-256 is vulnerable to `length-extension attacks <https://crypto.stackexchange.com/questions/3978/understanding-the-length-extension-attack>`_, |
| 17 |
which are relevant if you are computing the hash of a secret message. |
| 18 |
|
| 19 |
For instance, let's say you were planning to build a cheap MAC by concatenating a secret *key* to |
| 20 |
a public message *m* (bad idea!): |
| 21 |
|
| 22 |
.. math:: |
| 23 |
h = \text{SHA-256}(m || k) |
| 24 |
|
| 25 |
By only knowing the digest *h* and the length of *m* and *k*, the attacker can easily compute a second digest *h'*: |
| 26 |
|
| 27 |
.. math:: |
| 28 |
h' = \text{SHA-256}(m || p || z) |
| 29 |
|
| 30 |
where *p* is a well-known bit string and the attacker can pick a bit string *z* at will. |
| 31 |
|
| 32 |
.. _SHA-2: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf |
| 33 |
|
| 34 |
.. automodule:: Crypto.Hash.SHA256 |
| 35 |
:members: |