libmd5.tex 2.08 KB
Newer Older
1
\section{Built-in Module \sectcode{md5}}
Guido van Rossum's avatar
Guido van Rossum committed
2
\label{module-md5}
3 4 5
\bimodindex{md5}

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

For example, to obtain the digest of the string {\tt"Nobody inspects
the spammish repetition"}:
16

17
\begin{verbatim}
18 19
>>> import md5
>>> m = md5.new()
20 21
>>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition")
22
>>> m.digest()
23
'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
24
\end{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
25
%
26 27
More condensed:

28
\begin{verbatim}
29 30
>>> md5.new("Nobody inspects the spammish repetition").digest()
'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
31
\end{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
32
%
33
\setindexsubitem{(in module md5)}
34 35

\begin{funcdesc}{new}{\optional{arg}}
36 37
Return a new md5 object.  If \var{arg} is present, the method call
\code{update(\var{arg})} is made.
38 39
\end{funcdesc}

40 41
\begin{funcdesc}{md5}{\optional{arg}}
For backward compatibility reasons, this is an alternative name for the
42
\code{new()} function.
43 44
\end{funcdesc}

45
An md5 object has the following methods:
46

47
\setindexsubitem{(md5 method)}
48
\begin{funcdesc}{update}{arg}
49 50 51 52
Update the md5 object with the string \var{arg}.  Repeated calls are
equivalent to a single call with the concatenation of all the
arguments, i.e.\ \code{m.update(a); m.update(b)} is equivalent to
\code{m.update(a+b)}.
53 54 55
\end{funcdesc}

\begin{funcdesc}{digest}{}
56
Return the digest of the strings passed to the \code{update()}
57
method so far.  This is an 16-byte string which may contain
58
non-\ASCII{} characters, including null bytes.
59 60 61
\end{funcdesc}

\begin{funcdesc}{copy}{}
62 63 64
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.
65
\end{funcdesc}