blob: eac1a618f0995b3f36fa2939df5d35ee39ead01c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{md5} ---
Fred Drake1c7cd631999-04-23 22:03:00 +00002 MD5 message digest algorithm}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake1c7cd631999-04-23 22:03:00 +00004\declaremodule{builtin}{md5}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{RSA's MD5 message digest algorithm.}
6
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
8This module implements the interface to RSA's MD5 message digest
Fred Drake81acc2e1998-04-04 06:35:41 +00009\index{message digest, MD5}
Fred Drakec5891241998-02-09 19:16:20 +000010algorithm (see also Internet \rfc{1321}). Its use is quite
Fred Drake81acc2e1998-04-04 06:35:41 +000011straightforward:\ use the \function{new()} to create an md5 object.
Guido van Rossum470be141995-03-17 16:07:09 +000012You can now feed this object with arbitrary strings using the
Fred Drake81acc2e1998-04-04 06:35:41 +000013\method{update()} method, and at any point you can ask it for the
Guido van Rossum470be141995-03-17 16:07:09 +000014\dfn{digest} (a strong kind of 128-bit checksum,
15a.k.a. ``fingerprint'') of the contatenation of the strings fed to it
Fred Drake81acc2e1998-04-04 06:35:41 +000016so far using the \method{digest()} method.
17\index{checksum!MD5}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018
Fred Drake81acc2e1998-04-04 06:35:41 +000019For example, to obtain the digest of the string \code{'Nobody inspects
20the spammish repetition'}:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021
Fred Drake19479911998-02-13 06:58:54 +000022\begin{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000023>>> import md5
24>>> m = md5.new()
Guido van Rossum470be141995-03-17 16:07:09 +000025>>> m.update("Nobody inspects")
26>>> m.update(" the spammish repetition")
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000027>>> m.digest()
Guido van Rossum470be141995-03-17 16:07:09 +000028'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Fred Drake19479911998-02-13 06:58:54 +000029\end{verbatim}
Fred Drake81acc2e1998-04-04 06:35:41 +000030
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031More condensed:
32
Fred Drake19479911998-02-13 06:58:54 +000033\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000034>>> md5.new("Nobody inspects the spammish repetition").digest()
35'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Fred Drake19479911998-02-13 06:58:54 +000036\end{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000037
38\begin{funcdesc}{new}{\optional{arg}}
Guido van Rossum470be141995-03-17 16:07:09 +000039Return a new md5 object. If \var{arg} is present, the method call
40\code{update(\var{arg})} is made.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041\end{funcdesc}
42
Guido van Rossum31cce971995-01-04 19:17:34 +000043\begin{funcdesc}{md5}{\optional{arg}}
44For backward compatibility reasons, this is an alternative name for the
Fred Drake81acc2e1998-04-04 06:35:41 +000045\function{new()} function.
Guido van Rossum31cce971995-01-04 19:17:34 +000046\end{funcdesc}
47
Guido van Rossum470be141995-03-17 16:07:09 +000048An md5 object has the following methods:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049
Fred Drake81acc2e1998-04-04 06:35:41 +000050\begin{methoddesc}[md5]{update}{arg}
Guido van Rossum470be141995-03-17 16:07:09 +000051Update the md5 object with the string \var{arg}. Repeated calls are
52equivalent to a single call with the concatenation of all the
53arguments, i.e.\ \code{m.update(a); m.update(b)} is equivalent to
54\code{m.update(a+b)}.
Fred Drake81acc2e1998-04-04 06:35:41 +000055\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056
Fred Drake81acc2e1998-04-04 06:35:41 +000057\begin{methoddesc}[md5]{digest}{}
58Return the digest of the strings passed to the \method{update()}
Guido van Rossumdb9a7bb1996-06-26 19:21:58 +000059method so far. This is an 16-byte string which may contain
Guido van Rossum470be141995-03-17 16:07:09 +000060non-\ASCII{} characters, including null bytes.
Fred Drake81acc2e1998-04-04 06:35:41 +000061\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062
Fred Drake81acc2e1998-04-04 06:35:41 +000063\begin{methoddesc}[md5]{copy}{}
Guido van Rossum470be141995-03-17 16:07:09 +000064Return a copy (``clone'') of the md5 object. This can be used to
65efficiently compute the digests of strings that share a common initial
66substring.
Fred Drake81acc2e1998-04-04 06:35:41 +000067\end{methoddesc}