Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{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 | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 6 | algorithm (see also Internet \rfc{1321}). Its use is quite |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 7 | straightforward:\ use the \code{md5.new()} to create an md5 object. |
| 8 | You can now feed this object with arbitrary strings using the |
| 9 | \code{update()} method, and at any point you can ask it for the |
| 10 | \dfn{digest} (a strong kind of 128-bit checksum, |
| 11 | a.k.a. ``fingerprint'') of the contatenation of the strings fed to it |
| 12 | so far using the \code{digest()} method. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 14 | For example, to obtain the digest of the string {\tt"Nobody inspects |
| 15 | the spammish repetition"}: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 16 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 17 | \begin{verbatim} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 18 | >>> import md5 |
| 19 | >>> m = md5.new() |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 20 | >>> m.update("Nobody inspects") |
| 21 | >>> m.update(" the spammish repetition") |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 22 | >>> m.digest() |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 23 | '\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] | 24 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 25 | % |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 26 | More condensed: |
| 27 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 28 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 29 | >>> md5.new("Nobody inspects the spammish repetition").digest() |
| 30 | '\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] | 31 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 32 | % |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 33 | \setindexsubitem{(in module md5)} |
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 |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 42 | \code{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 | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 47 | \setindexsubitem{(md5 method)} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 48 | \begin{funcdesc}{update}{arg} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 49 | Update the md5 object with the string \var{arg}. Repeated calls are |
| 50 | equivalent to a single call with the concatenation of all the |
| 51 | arguments, i.e.\ \code{m.update(a); m.update(b)} is equivalent to |
| 52 | \code{m.update(a+b)}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | \end{funcdesc} |
| 54 | |
| 55 | \begin{funcdesc}{digest}{} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 56 | Return the digest of the strings passed to the \code{update()} |
Guido van Rossum | db9a7bb | 1996-06-26 19:21:58 +0000 | [diff] [blame] | 57 | 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] | 58 | non-\ASCII{} characters, including null bytes. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | \end{funcdesc} |
| 60 | |
| 61 | \begin{funcdesc}{copy}{} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 62 | Return a copy (``clone'') of the md5 object. This can be used to |
| 63 | efficiently compute the digests of strings that share a common initial |
| 64 | substring. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 65 | \end{funcdesc} |