Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 1 | \section{Built-in Module \module{md5}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-md5} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 3 | \bimodindex{md5} |
| 4 | |
| 5 | This module implements the interface to RSA's MD5 message digest |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 6 | \index{message digest, MD5} |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 7 | algorithm (see also Internet \rfc{1321}). Its use is quite |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 8 | straightforward:\ use the \function{new()} to create an md5 object. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 9 | You can now feed this object with arbitrary strings using the |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 10 | \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] | 11 | \dfn{digest} (a strong kind of 128-bit checksum, |
| 12 | a.k.a. ``fingerprint'') of the contatenation of the strings fed to it |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 13 | so far using the \method{digest()} method. |
| 14 | \index{checksum!MD5} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 15 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 16 | For example, to obtain the digest of the string \code{'Nobody inspects |
| 17 | the spammish repetition'}: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 18 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 19 | \begin{verbatim} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 20 | >>> import md5 |
| 21 | >>> m = md5.new() |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 22 | >>> m.update("Nobody inspects") |
| 23 | >>> m.update(" the spammish repetition") |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 | >>> m.digest() |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 25 | '\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 26 | \end{verbatim} |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | More condensed: |
| 29 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 30 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 31 | >>> md5.new("Nobody inspects the spammish repetition").digest() |
| 32 | '\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 33 | \end{verbatim} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 34 | |
| 35 | \begin{funcdesc}{new}{\optional{arg}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 36 | Return a new md5 object. If \var{arg} is present, the method call |
| 37 | \code{update(\var{arg})} is made. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 38 | \end{funcdesc} |
| 39 | |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 40 | \begin{funcdesc}{md5}{\optional{arg}} |
| 41 | For backward compatibility reasons, this is an alternative name for the |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 42 | \function{new()} function. |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 43 | \end{funcdesc} |
| 44 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 45 | An md5 object has the following methods: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 46 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 47 | \begin{methoddesc}[md5]{update}{arg} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 48 | Update the md5 object with the string \var{arg}. Repeated calls are |
| 49 | equivalent to a single call with the concatenation of all the |
| 50 | arguments, i.e.\ \code{m.update(a); m.update(b)} is equivalent to |
| 51 | \code{m.update(a+b)}. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 52 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 54 | \begin{methoddesc}[md5]{digest}{} |
| 55 | Return the digest of the strings passed to the \method{update()} |
Guido van Rossum | db9a7bb | 1996-06-26 19:21:58 +0000 | [diff] [blame] | 56 | method so far. This is an 16-byte string which may contain |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 57 | non-\ASCII{} characters, including null bytes. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 58 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 60 | \begin{methoddesc}[md5]{copy}{} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 61 | Return a copy (``clone'') of the md5 object. This can be used to |
| 62 | efficiently compute the digests of strings that share a common initial |
| 63 | substring. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 64 | \end{methoddesc} |