| 1 |
SHA-1 |
| 2 |
===== |
| 3 |
|
| 4 |
SHA-1_ produces the 160 bit digest of a message. |
| 5 |
For example:: |
| 6 |
|
| 7 |
>>> from Crypto.Hash import SHA1 |
| 8 |
>>> |
| 9 |
>>> h = SHA1.new() |
| 10 |
>>> h.update(b'Hello') |
| 11 |
>>> print h.hexdigest() |
| 12 |
|
| 13 |
*SHA* stands for Secure Hash Algorithm. |
| 14 |
|
| 15 |
.. warning:: |
| 16 |
This algorithm is not considered secure. Do not use it for new designs. |
| 17 |
|
| 18 |
.. warning:: |
| 19 |
SHA-1 is vulnerable to `length-extension attacks <https://crypto.stackexchange.com/questions/3978/understanding-the-length-extension-attack>`_, |
| 20 |
which are relevant if you are computing the hash of a secret message. |
| 21 |
|
| 22 |
For instance, let's say you were planning to build a cheap MAC by concatenating a secret *key* to |
| 23 |
a public message *m* (bad idea!): |
| 24 |
|
| 25 |
.. math:: |
| 26 |
h = \text{SHA-1}(m || k) |
| 27 |
|
| 28 |
By only knowing the digest *h* and the length of *m* and *k*, the attacker can easily compute a second digest *h'*: |
| 29 |
|
| 30 |
.. math:: |
| 31 |
h' = \text{SHA-1}(m || p || z) |
| 32 |
|
| 33 |
where *p* is a well-known bit string and the attacker can pick a bit string *z* at will. |
| 34 |
|
| 35 |
.. _SHA-1: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 36 |
|
| 37 |
.. py:function:: Crypto.SHA1.new(msg=None) |
| 38 |
|
| 39 |
Create a new hash object. |
| 40 |
|
| 41 |
:param msg: |
| 42 |
Optional. The very first chunk of the message to hash. |
| 43 |
It is equivalent to an early call to :func:`update`. |
| 44 |
:type msg: byte string |
| 45 |
|
| 46 |
:returns: A :class:`SHA1_Hash` hash object |
| 47 |
|
| 48 |
.. py:class:: Crypto.SHA1.SHA1_Hash |
| 49 |
|
| 50 |
A SHA-1 hash object. |
| 51 |
Do not instantiate directly. |
| 52 |
Use the :func:`new` function. |
| 53 |
|
| 54 |
:ivar oid: ASN.1 Object ID |
| 55 |
:vartype oid: string |
| 56 |
|
| 57 |
:ivar block_size: the size in bytes of the internal message block, |
| 58 |
input to the compression function |
| 59 |
:vartype block_size: integer |
| 60 |
|
| 61 |
:ivar digest_size: the size in bytes of the resulting hash |
| 62 |
:vartype digest_size: integer |
| 63 |
|
| 64 |
.. py:method:: update(msg) |
| 65 |
|
| 66 |
Continue hashing of a message by consuming the next chunk of data. |
| 67 |
|
| 68 |
:param msg: The next chunk of the message being hashed. |
| 69 |
:type param: byte string |
| 70 |
|
| 71 |
.. py:method:: digest() |
| 72 |
|
| 73 |
Return the **binary** (non-printable) digest of the message that has been hashed so far. |
| 74 |
|
| 75 |
:return: The hash digest, computed over the data processed so far. |
| 76 |
Binary form. |
| 77 |
:rtype: byte string |
| 78 |
|
| 79 |
.. py:method:: hexdigest() |
| 80 |
|
| 81 |
Return the **printable** digest of the message that has been hashed so far. |
| 82 |
|
| 83 |
:return: The hash digest, computed over the data processed so far. |
| 84 |
Hexadecimal encoded. |
| 85 |
:rtype: string |
| 86 |
|
| 87 |
|