blob: 946d00d138775f5300349bf1f1b23ee9a754b8a0 [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
Fred Drakec5891241998-02-09 19:16:20 +00006algorithm (see also Internet \rfc{1321}). Its use is quite
Guido van Rossum470be141995-03-17 16:07:09 +00007straightforward:\ 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
Fred Drake19479911998-02-13 06:58:54 +000017\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'
Fred Drake19479911998-02-13 06:58:54 +000024\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000025%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026More condensed:
27
Fred Drake19479911998-02-13 06:58:54 +000028\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'
Fred Drake19479911998-02-13 06:58:54 +000031\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000032%
Fred Drake19479911998-02-13 06:58:54 +000033\setindexsubitem{(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
Fred Drake19479911998-02-13 06:58:54 +000047\setindexsubitem{(md5 method)}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\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}