blob: 4408372a2ed0731fd73b58805a18b72ac4bd549a [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`md5` --- MD5 message digest algorithm
3===========================================
4
5.. module:: md5
6 :synopsis: RSA's MD5 message digest algorithm.
7
8
9.. deprecated:: 2.5
10 Use the :mod:`hashlib` module instead.
11
12.. index::
13 single: message digest, MD5
14 single: checksum; MD5
15
16This module implements the interface to RSA's MD5 message digest algorithm (see
17also Internet :rfc:`1321`). Its use is quite straightforward: use :func:`new`
18to create an md5 object. You can now feed this object with arbitrary strings
19using the :meth:`update` method, and at any point you can ask it for the
20:dfn:`digest` (a strong kind of 128-bit checksum, a.k.a. "fingerprint") of the
21concatenation of the strings fed to it so far using the :meth:`digest` method.
22
23For example, to obtain the digest of the string ``'Nobody inspects the spammish
24repetition'``::
25
26 >>> import md5
27 >>> m = md5.new()
28 >>> m.update("Nobody inspects")
29 >>> m.update(" the spammish repetition")
30 >>> m.digest()
31 '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
32
33More condensed::
34
35 >>> md5.new("Nobody inspects the spammish repetition").digest()
36 '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
37
38The following values are provided as constants in the module and as attributes
39of the md5 objects returned by :func:`new`:
40
41
42.. data:: digest_size
43
44 The size of the resulting digest in bytes. This is always ``16``.
45
46The md5 module provides the following functions:
47
48
49.. function:: new([arg])
50
51 Return a new md5 object. If *arg* is present, the method call ``update(arg)``
52 is made.
53
54
55.. function:: md5([arg])
56
57 For backward compatibility reasons, this is an alternative name for the
58 :func:`new` function.
59
60An md5 object has the following methods:
61
62
63.. method:: md5.update(arg)
64
65 Update the md5 object with the string *arg*. Repeated calls are equivalent to a
66 single call with the concatenation of all the arguments: ``m.update(a);
67 m.update(b)`` is equivalent to ``m.update(a+b)``.
68
69
70.. method:: md5.digest()
71
72 Return the digest of the strings passed to the :meth:`update` method so far.
73 This is a 16-byte string which may contain non-ASCII characters, including null
74 bytes.
75
76
77.. method:: md5.hexdigest()
78
79 Like :meth:`digest` except the digest is returned as a string of length 32,
80 containing only hexadecimal digits. This may be used to exchange the value
81 safely in email or other non-binary environments.
82
83
84.. method:: md5.copy()
85
86 Return a copy ("clone") of the md5 object. This can be used to efficiently
87 compute the digests of strings that share a common initial substring.
88
89
90.. seealso::
91
92 Module :mod:`sha`
93 Similar module implementing the Secure Hash Algorithm (SHA). The SHA algorithm
94 is considered a more secure hash.
95