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 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 7 | |
| 8 | This module implements the interface to RSA's MD5 message digest |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 9 | \index{message digest, MD5} |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 10 | algorithm (see also Internet \rfc{1321}). Its use is quite |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 11 | straightforward:\ use \function{new()} to create an md5 object. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 12 | You can now feed this object with arbitrary strings using the |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 13 | \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] | 14 | \dfn{digest} (a strong kind of 128-bit checksum, |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 15 | 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] | 16 | so far using the \method{digest()} method. |
| 17 | \index{checksum!MD5} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 18 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 19 | For example, to obtain the digest of the string \code{'Nobody inspects |
| 20 | the spammish repetition'}: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 21 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 22 | \begin{verbatim} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 23 | >>> import md5 |
| 24 | >>> m = md5.new() |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 25 | >>> m.update("Nobody inspects") |
| 26 | >>> m.update(" the spammish repetition") |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 27 | >>> m.digest() |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 28 | '\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] | 29 | \end{verbatim} |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 31 | More condensed: |
| 32 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 33 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 34 | >>> md5.new("Nobody inspects the spammish repetition").digest() |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 35 | '\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] | 36 | \end{verbatim} |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 37 | |
| 38 | \begin{funcdesc}{new}{\optional{arg}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 39 | Return a new md5 object. If \var{arg} is present, the method call |
| 40 | \code{update(\var{arg})} is made. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 41 | \end{funcdesc} |
| 42 | |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 43 | \begin{funcdesc}{md5}{\optional{arg}} |
| 44 | For backward compatibility reasons, this is an alternative name for the |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 45 | \function{new()} function. |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 46 | \end{funcdesc} |
| 47 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 48 | An md5 object has the following methods: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 49 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 50 | \begin{methoddesc}[md5]{update}{arg} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 51 | Update the md5 object with the string \var{arg}. Repeated calls are |
| 52 | equivalent to a single call with the concatenation of all the |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 53 | arguments: \code{m.update(a); m.update(b)} is equivalent to |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 54 | \code{m.update(a+b)}. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 55 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 56 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 57 | \begin{methoddesc}[md5]{digest}{} |
| 58 | Return the digest of the strings passed to the \method{update()} |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 59 | 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] | 60 | non-\ASCII{} characters, including null bytes. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 61 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 62 | |
Barry Warsaw | 4ef4be5 | 2000-08-15 06:00:28 +0000 | [diff] [blame] | 63 | \begin{methoddesc}[md5]{hexdigest}{} |
| 64 | Like \method{digest()} except the digest is returned as a string of |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 65 | length 32, containing only hexadecimal digits. This may |
| 66 | be used to exchange the value safely in email or other non-binary |
| 67 | environments. |
Barry Warsaw | 4ef4be5 | 2000-08-15 06:00:28 +0000 | [diff] [blame] | 68 | \end{methoddesc} |
| 69 | |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 70 | \begin{methoddesc}[md5]{copy}{} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 71 | Return a copy (``clone'') of the md5 object. This can be used to |
| 72 | efficiently compute the digests of strings that share a common initial |
| 73 | substring. |
Fred Drake | 81acc2e | 1998-04-04 06:35:41 +0000 | [diff] [blame] | 74 | \end{methoddesc} |
Fred Drake | 4dfad57 | 2000-09-14 21:47:32 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | \begin{seealso} |
| 78 | \seemodule{sha}{Similar module implementing the Secure Hash |
| 79 | Algorithm (SHA). The SHA algorithm is considered a |
| 80 | more secure hash.} |
| 81 | \end{seealso} |