blob: bd26f0205263e1fb9b15255f07177687bf02b523 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Built-in Module \sectcode{md5}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00002\bimodindex{md5}
3
4This module implements the interface to RSA's MD5 message digest
Guido van Rossum470be141995-03-17 16:07:09 +00005algorithm (see also Internet RFC 1321). Its use is quite
6straightforward:\ use the \code{md5.new()} to create an md5 object.
7You can now feed this object with arbitrary strings using the
8\code{update()} method, and at any point you can ask it for the
9\dfn{digest} (a strong kind of 128-bit checksum,
10a.k.a. ``fingerprint'') of the contatenation of the strings fed to it
11so far using the \code{digest()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012
Guido van Rossum470be141995-03-17 16:07:09 +000013For example, to obtain the digest of the string {\tt"Nobody inspects
14the spammish repetition"}:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015
16\bcode\begin{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000017>>> import md5
18>>> m = md5.new()
Guido van Rossum470be141995-03-17 16:07:09 +000019>>> m.update("Nobody inspects")
20>>> m.update(" the spammish repetition")
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021>>> m.digest()
Guido van Rossum470be141995-03-17 16:07:09 +000022'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023\end{verbatim}\ecode
24
25More condensed:
26
27\bcode\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000028>>> md5.new("Nobody inspects the spammish repetition").digest()
29'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030\end{verbatim}\ecode
31
32\renewcommand{\indexsubitem}{(in module md5)}
Guido van Rossum31cce971995-01-04 19:17:34 +000033
34\begin{funcdesc}{new}{\optional{arg}}
Guido van Rossum470be141995-03-17 16:07:09 +000035Return a new md5 object. If \var{arg} is present, the method call
36\code{update(\var{arg})} is made.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000037\end{funcdesc}
38
Guido van Rossum31cce971995-01-04 19:17:34 +000039\begin{funcdesc}{md5}{\optional{arg}}
40For backward compatibility reasons, this is an alternative name for the
Guido van Rossum470be141995-03-17 16:07:09 +000041\code{new()} function.
Guido van Rossum31cce971995-01-04 19:17:34 +000042\end{funcdesc}
43
Guido van Rossum470be141995-03-17 16:07:09 +000044An md5 object has the following methods:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045
46\renewcommand{\indexsubitem}{(md5 method)}
47\begin{funcdesc}{update}{arg}
Guido van Rossum470be141995-03-17 16:07:09 +000048Update the md5 object with the string \var{arg}. Repeated calls are
49equivalent to a single call with the concatenation of all the
50arguments, i.e.\ \code{m.update(a); m.update(b)} is equivalent to
51\code{m.update(a+b)}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\end{funcdesc}
53
54\begin{funcdesc}{digest}{}
Guido van Rossum470be141995-03-17 16:07:09 +000055Return the digest of the strings passed to the \code{update()}
56method so far. This is an 8-byte string which may contain
57non-\ASCII{} characters, including null bytes.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\end{funcdesc}
59
60\begin{funcdesc}{copy}{}
Guido van Rossum470be141995-03-17 16:07:09 +000061Return a copy (``clone'') of the md5 object. This can be used to
62efficiently compute the digests of strings that share a common initial
63substring.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064\end{funcdesc}