blob: d71bacda08cff11f5f1020d471bff08c5836180e [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Built-in Module \sectcode{md5}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-md5}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{md5}
4
5This module implements the interface to RSA's MD5 message digest
Guido van Rossum470be141995-03-17 16:07:09 +00006algorithm (see also Internet RFC 1321). Its use is quite
7straightforward:\ use the \code{md5.new()} to create an md5 object.
8You 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,
11a.k.a. ``fingerprint'') of the contatenation of the strings fed to it
12so far using the \code{digest()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013
Guido van Rossum470be141995-03-17 16:07:09 +000014For example, to obtain the digest of the string {\tt"Nobody inspects
15the spammish repetition"}:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016
17\bcode\begin{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000018>>> import md5
19>>> m = md5.new()
Guido van Rossum470be141995-03-17 16:07:09 +000020>>> m.update("Nobody inspects")
21>>> m.update(" the spammish repetition")
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022>>> m.digest()
Guido van Rossum470be141995-03-17 16:07:09 +000023'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024\end{verbatim}\ecode
Guido van Rossume47da0a1997-07-17 16:34:52 +000025%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026More condensed:
27
28\bcode\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000029>>> md5.new("Nobody inspects the spammish repetition").digest()
30'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031\end{verbatim}\ecode
Guido van Rossume47da0a1997-07-17 16:34:52 +000032%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033\renewcommand{\indexsubitem}{(in module md5)}
Guido van Rossum31cce971995-01-04 19:17:34 +000034
35\begin{funcdesc}{new}{\optional{arg}}
Guido van Rossum470be141995-03-17 16:07:09 +000036Return a new md5 object. If \var{arg} is present, the method call
37\code{update(\var{arg})} is made.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038\end{funcdesc}
39
Guido van Rossum31cce971995-01-04 19:17:34 +000040\begin{funcdesc}{md5}{\optional{arg}}
41For backward compatibility reasons, this is an alternative name for the
Guido van Rossum470be141995-03-17 16:07:09 +000042\code{new()} function.
Guido van Rossum31cce971995-01-04 19:17:34 +000043\end{funcdesc}
44
Guido van Rossum470be141995-03-17 16:07:09 +000045An md5 object has the following methods:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046
47\renewcommand{\indexsubitem}{(md5 method)}
48\begin{funcdesc}{update}{arg}
Guido van Rossum470be141995-03-17 16:07:09 +000049Update the md5 object with the string \var{arg}. Repeated calls are
50equivalent to a single call with the concatenation of all the
51arguments, i.e.\ \code{m.update(a); m.update(b)} is equivalent to
52\code{m.update(a+b)}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053\end{funcdesc}
54
55\begin{funcdesc}{digest}{}
Guido van Rossum470be141995-03-17 16:07:09 +000056Return the digest of the strings passed to the \code{update()}
Guido van Rossumdb9a7bb1996-06-26 19:21:58 +000057method so far. This is an 16-byte string which may contain
Guido van Rossum470be141995-03-17 16:07:09 +000058non-\ASCII{} characters, including null bytes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059\end{funcdesc}
60
61\begin{funcdesc}{copy}{}
Guido van Rossum470be141995-03-17 16:07:09 +000062Return a copy (``clone'') of the md5 object. This can be used to
63efficiently compute the digests of strings that share a common initial
64substring.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065\end{funcdesc}