blob: d1e73670e40e1272d6272c3a8d903a04d8805cd7 [file] [log] [blame]
Fred Drake81acc2e1998-04-04 06:35:41 +00001\section{Built-in Module \module{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 Drake81acc2e1998-04-04 06:35:41 +00006\index{message digest, MD5}
Fred Drakec5891241998-02-09 19:16:20 +00007algorithm (see also Internet \rfc{1321}). Its use is quite
Fred Drake81acc2e1998-04-04 06:35:41 +00008straightforward:\ use the \function{new()} to create an md5 object.
Guido van Rossum470be141995-03-17 16:07:09 +00009You can now feed this object with arbitrary strings using the
Fred Drake81acc2e1998-04-04 06:35:41 +000010\method{update()} method, and at any point you can ask it for the
Guido van Rossum470be141995-03-17 16:07:09 +000011\dfn{digest} (a strong kind of 128-bit checksum,
12a.k.a. ``fingerprint'') of the contatenation of the strings fed to it
Fred Drake81acc2e1998-04-04 06:35:41 +000013so far using the \method{digest()} method.
14\index{checksum!MD5}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015
Fred Drake81acc2e1998-04-04 06:35:41 +000016For example, to obtain the digest of the string \code{'Nobody inspects
17the spammish repetition'}:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018
Fred Drake19479911998-02-13 06:58:54 +000019\begin{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000020>>> import md5
21>>> m = md5.new()
Guido van Rossum470be141995-03-17 16:07:09 +000022>>> m.update("Nobody inspects")
23>>> m.update(" the spammish repetition")
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024>>> m.digest()
Guido van Rossum470be141995-03-17 16:07:09 +000025'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Fred Drake19479911998-02-13 06:58:54 +000026\end{verbatim}
Fred Drake81acc2e1998-04-04 06:35:41 +000027
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028More condensed:
29
Fred Drake19479911998-02-13 06:58:54 +000030\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000031>>> md5.new("Nobody inspects the spammish repetition").digest()
32'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Fred Drake19479911998-02-13 06:58:54 +000033\end{verbatim}
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
Fred Drake81acc2e1998-04-04 06:35:41 +000042\function{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 Drake81acc2e1998-04-04 06:35:41 +000047\begin{methoddesc}[md5]{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)}.
Fred Drake81acc2e1998-04-04 06:35:41 +000052\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053
Fred Drake81acc2e1998-04-04 06:35:41 +000054\begin{methoddesc}[md5]{digest}{}
55Return the digest of the strings passed to the \method{update()}
Guido van Rossumdb9a7bb1996-06-26 19:21:58 +000056method so far. This is an 16-byte string which may contain
Guido van Rossum470be141995-03-17 16:07:09 +000057non-\ASCII{} characters, including null bytes.
Fred Drake81acc2e1998-04-04 06:35:41 +000058\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059
Fred Drake81acc2e1998-04-04 06:35:41 +000060\begin{methoddesc}[md5]{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.
Fred Drake81acc2e1998-04-04 06:35:41 +000064\end{methoddesc}