Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{md5} --- |
Fred Drake | 1c7cd63 | 1999-04-23 22:03:00 +0000 | [diff] [blame] | 2 | MD5 message digest algorithm} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 1c7cd63 | 1999-04-23 22:03:00 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{md5} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{RSA's MD5 message digest algorithm.} |
| 6 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 7 | \deprecated{2.5}{Use the \refmodule{hashlib} module instead.} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | |
| 9 | This module implements the interface to RSA's MD5 message digest |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 10 | \index{message digest, MD5} |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 11 | algorithm (see also Internet \rfc{1321}). Its use is quite |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 12 | straightforward:\ use \function{new()} to create an md5 object. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 13 | You can now feed this object with arbitrary strings using the |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 14 | \method{update()} method, and at any point you can ask it for the |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 15 | \dfn{digest} (a strong kind of 128-bit checksum, |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 16 | a.k.a. ``fingerprint'') of the concatenation of the strings fed to it |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 17 | so far using the \method{digest()} method. |
| 18 | \index{checksum!MD5} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 19 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 20 | For example, to obtain the digest of the string \code{'Nobody inspects |
| 21 | the spammish repetition'}: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 22 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 23 | \begin{verbatim} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 24 | >>> import md5 |
| 25 | >>> m = md5.new() |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 26 | >>> m.update("Nobody inspects") |
| 27 | >>> m.update(" the spammish repetition") |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | >>> m.digest() |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 29 | '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 30 | \end{verbatim} |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 32 | More condensed: |
| 33 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 34 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 35 | >>> md5.new("Nobody inspects the spammish repetition").digest() |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 36 | '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 37 | \end{verbatim} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 38 | |
Andrew M. Kuchling | bc4a1c2 | 2001-11-02 21:44:09 +0000 | [diff] [blame] | 39 | The following values are provided as constants in the module and as |
| 40 | attributes of the md5 objects returned by \function{new()}: |
| 41 | |
| 42 | \begin{datadesc}{digest_size} |
| 43 | The size of the resulting digest in bytes. This is always |
| 44 | \code{16}. |
| 45 | \end{datadesc} |
| 46 | |
Georg Brandl | 5e8d8f9 | 2005-12-22 16:15:00 +0000 | [diff] [blame] | 47 | The md5 module provides the following functions: |
Andrew M. Kuchling | bc4a1c2 | 2001-11-02 21:44:09 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 49 | \begin{funcdesc}{new}{\optional{arg}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 50 | Return a new md5 object. If \var{arg} is present, the method call |
| 51 | \code{update(\var{arg})} is made. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 52 | \end{funcdesc} |
| 53 | |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 54 | \begin{funcdesc}{md5}{\optional{arg}} |
| 55 | For backward compatibility reasons, this is an alternative name for the |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 56 | \function{new()} function. |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 57 | \end{funcdesc} |
| 58 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 59 | An md5 object has the following methods: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 60 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 61 | \begin{methoddesc}[md5]{update}{arg} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 62 | Update the md5 object with the string \var{arg}. Repeated calls are |
| 63 | equivalent to a single call with the concatenation of all the |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 64 | arguments: \code{m.update(a); m.update(b)} is equivalent to |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 65 | \code{m.update(a+b)}. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 66 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 68 | \begin{methoddesc}[md5]{digest}{} |
| 69 | Return the digest of the strings passed to the \method{update()} |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 70 | method so far. This is a 16-byte string which may contain |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 71 | non-\ASCII{} characters, including null bytes. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 72 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 73 | |
Barry Warsaw | 4ef4be5 | 2000-08-15 06:00:28 +0000 | [diff] [blame] | 74 | \begin{methoddesc}[md5]{hexdigest}{} |
| 75 | Like \method{digest()} except the digest is returned as a string of |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 76 | length 32, containing only hexadecimal digits. This may |
| 77 | be used to exchange the value safely in email or other non-binary |
| 78 | environments. |
Barry Warsaw | 4ef4be5 | 2000-08-15 06:00:28 +0000 | [diff] [blame] | 79 | \end{methoddesc} |
| 80 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 81 | \begin{methoddesc}[md5]{copy}{} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 82 | Return a copy (``clone'') of the md5 object. This can be used to |
| 83 | efficiently compute the digests of strings that share a common initial |
| 84 | substring. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 85 | \end{methoddesc} |
Fred Drake | 4dfad57 | 2000-09-14 21:47:32 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | \begin{seealso} |
| 89 | \seemodule{sha}{Similar module implementing the Secure Hash |
| 90 | Algorithm (SHA). The SHA algorithm is considered a |
| 91 | more secure hash.} |
| 92 | \end{seealso} |