| 1 | \section{\module{md5} ---
|
|---|
| 2 | MD5 message digest algorithm}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{builtin}{md5}
|
|---|
| 5 | \modulesynopsis{RSA's MD5 message digest algorithm.}
|
|---|
| 6 |
|
|---|
| 7 | \deprecated{2.5}{Use the \refmodule{hashlib} module instead.}
|
|---|
| 8 |
|
|---|
| 9 | This module implements the interface to RSA's MD5 message digest
|
|---|
| 10 | \index{message digest, MD5}
|
|---|
| 11 | algorithm (see also Internet \rfc{1321}). Its use is quite
|
|---|
| 12 | straightforward:\ use \function{new()} to create an md5 object.
|
|---|
| 13 | You can now feed this object with arbitrary strings using the
|
|---|
| 14 | \method{update()} method, and at any point you can ask it for the
|
|---|
| 15 | \dfn{digest} (a strong kind of 128-bit checksum,
|
|---|
| 16 | a.k.a. ``fingerprint'') of the concatenation of the strings fed to it
|
|---|
| 17 | so far using the \method{digest()} method.
|
|---|
| 18 | \index{checksum!MD5}
|
|---|
| 19 |
|
|---|
| 20 | For example, to obtain the digest of the string \code{'Nobody inspects
|
|---|
| 21 | the spammish repetition'}:
|
|---|
| 22 |
|
|---|
| 23 | \begin{verbatim}
|
|---|
| 24 | >>> import md5
|
|---|
| 25 | >>> m = md5.new()
|
|---|
| 26 | >>> m.update("Nobody inspects")
|
|---|
| 27 | >>> m.update(" the spammish repetition")
|
|---|
| 28 | >>> m.digest()
|
|---|
| 29 | '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
|
|---|
| 30 | \end{verbatim}
|
|---|
| 31 |
|
|---|
| 32 | More condensed:
|
|---|
| 33 |
|
|---|
| 34 | \begin{verbatim}
|
|---|
| 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 | \end{verbatim}
|
|---|
| 38 |
|
|---|
| 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 |
|
|---|
| 47 | The md5 module provides the following functions:
|
|---|
| 48 |
|
|---|
| 49 | \begin{funcdesc}{new}{\optional{arg}}
|
|---|
| 50 | Return a new md5 object. If \var{arg} is present, the method call
|
|---|
| 51 | \code{update(\var{arg})} is made.
|
|---|
| 52 | \end{funcdesc}
|
|---|
| 53 |
|
|---|
| 54 | \begin{funcdesc}{md5}{\optional{arg}}
|
|---|
| 55 | For backward compatibility reasons, this is an alternative name for the
|
|---|
| 56 | \function{new()} function.
|
|---|
| 57 | \end{funcdesc}
|
|---|
| 58 |
|
|---|
| 59 | An md5 object has the following methods:
|
|---|
| 60 |
|
|---|
| 61 | \begin{methoddesc}[md5]{update}{arg}
|
|---|
| 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
|
|---|
| 64 | arguments: \code{m.update(a); m.update(b)} is equivalent to
|
|---|
| 65 | \code{m.update(a+b)}.
|
|---|
| 66 | \end{methoddesc}
|
|---|
| 67 |
|
|---|
| 68 | \begin{methoddesc}[md5]{digest}{}
|
|---|
| 69 | Return the digest of the strings passed to the \method{update()}
|
|---|
| 70 | method so far. This is a 16-byte string which may contain
|
|---|
| 71 | non-\ASCII{} characters, including null bytes.
|
|---|
| 72 | \end{methoddesc}
|
|---|
| 73 |
|
|---|
| 74 | \begin{methoddesc}[md5]{hexdigest}{}
|
|---|
| 75 | Like \method{digest()} except the digest is returned as a string of
|
|---|
| 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.
|
|---|
| 79 | \end{methoddesc}
|
|---|
| 80 |
|
|---|
| 81 | \begin{methoddesc}[md5]{copy}{}
|
|---|
| 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.
|
|---|
| 85 | \end{methoddesc}
|
|---|
| 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}
|
|---|