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