libmd5.tex 2.9 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1
\section{\module{md5} ---
Fred Drake's avatar
Fred Drake committed
2
         MD5 message digest algorithm}
3

Fred Drake's avatar
Fred Drake committed
4
\declaremodule{builtin}{md5}
5 6
\modulesynopsis{RSA's MD5 message digest algorithm.}

7 8

This module implements the interface to RSA's MD5 message digest
9
\index{message digest, MD5}
10
algorithm (see also Internet \rfc{1321}).  Its use is quite
11
straightforward:\ use \function{new()} to create an md5 object.
12
You can now feed this object with arbitrary strings using the
13
\method{update()} method, and at any point you can ask it for the
14
\dfn{digest} (a strong kind of 128-bit checksum,
15
a.k.a. ``fingerprint'') of the concatenation of the strings fed to it
16 17
so far using the \method{digest()} method.
\index{checksum!MD5}
18

19 20
For example, to obtain the digest of the string \code{'Nobody inspects
the spammish repetition'}:
21

22
\begin{verbatim}
23 24
>>> import md5
>>> m = md5.new()
25 26
>>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition")
27
>>> m.digest()
28
'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
29
\end{verbatim}
30

31 32
More condensed:

33
\begin{verbatim}
34
>>> md5.new("Nobody inspects the spammish repetition").digest()
35
'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
36
\end{verbatim}
37

38 39 40 41 42 43 44 45 46 47
The following values are provided as constants in the module and as
attributes of the md5 objects returned by \function{new()}:

\begin{datadesc}{digest_size}
  The size of the resulting digest in bytes.  This is always
  \code{16}.
\end{datadesc}

md5 objects support the following methods:

48
\begin{funcdesc}{new}{\optional{arg}}
49 50
Return a new md5 object.  If \var{arg} is present, the method call
\code{update(\var{arg})} is made.
51 52
\end{funcdesc}

53 54
\begin{funcdesc}{md5}{\optional{arg}}
For backward compatibility reasons, this is an alternative name for the
55
\function{new()} function.
56 57
\end{funcdesc}

58
An md5 object has the following methods:
59

60
\begin{methoddesc}[md5]{update}{arg}
61 62
Update the md5 object with the string \var{arg}.  Repeated calls are
equivalent to a single call with the concatenation of all the
63
arguments: \code{m.update(a); m.update(b)} is equivalent to
64
\code{m.update(a+b)}.
65
\end{methoddesc}
66

67 68
\begin{methoddesc}[md5]{digest}{}
Return the digest of the strings passed to the \method{update()}
69
method so far.  This is a 16-byte string which may contain
70
non-\ASCII{} characters, including null bytes.
71
\end{methoddesc}
72

73 74
\begin{methoddesc}[md5]{hexdigest}{}
Like \method{digest()} except the digest is returned as a string of
75 76 77
length 32, containing only hexadecimal digits.  This may 
be used to exchange the value safely in email or other non-binary
environments.
78 79
\end{methoddesc}

80
\begin{methoddesc}[md5]{copy}{}
81 82 83
Return a copy (``clone'') of the md5 object.  This can be used to
efficiently compute the digests of strings that share a common initial
substring.
84
\end{methoddesc}
85 86 87 88 89 90 91


\begin{seealso}
  \seemodule{sha}{Similar module implementing the Secure Hash
                  Algorithm (SHA).  The SHA algorithm is considered a
                  more secure hash.}
\end{seealso}