blob: 4d76b7c81534ba289c73906bee63072033014b4c [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}
Fred Drakec5891241998-02-09 19:16:20 +00003\rfcindex{1321}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00004\bimodindex{md5}
5
6This module implements the interface to RSA's MD5 message digest
Fred Drakec5891241998-02-09 19:16:20 +00007algorithm (see also Internet \rfc{1321}). Its use is quite
Guido van Rossum470be141995-03-17 16:07:09 +00008straightforward:\ use the \code{md5.new()} to create an md5 object.
9You can now feed this object with arbitrary strings using the
10\code{update()} method, and at any point you can ask it for the
11\dfn{digest} (a strong kind of 128-bit checksum,
12a.k.a. ``fingerprint'') of the contatenation of the strings fed to it
13so far using the \code{digest()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014
Guido van Rossum470be141995-03-17 16:07:09 +000015For example, to obtain the digest of the string {\tt"Nobody inspects
16the spammish repetition"}:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017
18\bcode\begin{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000019>>> import md5
20>>> m = md5.new()
Guido van Rossum470be141995-03-17 16:07:09 +000021>>> m.update("Nobody inspects")
22>>> m.update(" the spammish repetition")
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023>>> m.digest()
Guido van Rossum470be141995-03-17 16:07:09 +000024'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025\end{verbatim}\ecode
Guido van Rossume47da0a1997-07-17 16:34:52 +000026%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000027More condensed:
28
29\bcode\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000030>>> md5.new("Nobody inspects the spammish repetition").digest()
31'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032\end{verbatim}\ecode
Guido van Rossume47da0a1997-07-17 16:34:52 +000033%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034\renewcommand{\indexsubitem}{(in module md5)}
Guido van Rossum31cce971995-01-04 19:17:34 +000035
36\begin{funcdesc}{new}{\optional{arg}}
Guido van Rossum470be141995-03-17 16:07:09 +000037Return a new md5 object. If \var{arg} is present, the method call
38\code{update(\var{arg})} is made.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039\end{funcdesc}
40
Guido van Rossum31cce971995-01-04 19:17:34 +000041\begin{funcdesc}{md5}{\optional{arg}}
42For backward compatibility reasons, this is an alternative name for the
Guido van Rossum470be141995-03-17 16:07:09 +000043\code{new()} function.
Guido van Rossum31cce971995-01-04 19:17:34 +000044\end{funcdesc}
45
Guido van Rossum470be141995-03-17 16:07:09 +000046An md5 object has the following methods:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047
48\renewcommand{\indexsubitem}{(md5 method)}
49\begin{funcdesc}{update}{arg}
Guido van Rossum470be141995-03-17 16:07:09 +000050Update the md5 object with the string \var{arg}. Repeated calls are
51equivalent to a single call with the concatenation of all the
52arguments, i.e.\ \code{m.update(a); m.update(b)} is equivalent to
53\code{m.update(a+b)}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054\end{funcdesc}
55
56\begin{funcdesc}{digest}{}
Guido van Rossum470be141995-03-17 16:07:09 +000057Return the digest of the strings passed to the \code{update()}
Guido van Rossumdb9a7bb1996-06-26 19:21:58 +000058method so far. This is an 16-byte string which may contain
Guido van Rossum470be141995-03-17 16:07:09 +000059non-\ASCII{} characters, including null bytes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000060\end{funcdesc}
61
62\begin{funcdesc}{copy}{}
Guido van Rossum470be141995-03-17 16:07:09 +000063Return a copy (``clone'') of the md5 object. This can be used to
64efficiently compute the digests of strings that share a common initial
65substring.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066\end{funcdesc}