blob: b754a660deb71ca7ec2515f907f88678f8c60a16 [file] [log] [blame]
Fred Drake81acc2e1998-04-04 06:35:41 +00001\section{Built-in Module \module{md5}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{md5}
3
4\modulesynopsis{RSA's MD5 message digest algorithm.}
5
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006
7This module implements the interface to RSA's MD5 message digest
Fred Drake81acc2e1998-04-04 06:35:41 +00008\index{message digest, MD5}
Fred Drakec5891241998-02-09 19:16:20 +00009algorithm (see also Internet \rfc{1321}). Its use is quite
Fred Drake81acc2e1998-04-04 06:35:41 +000010straightforward:\ use the \function{new()} to create an md5 object.
Guido van Rossum470be141995-03-17 16:07:09 +000011You can now feed this object with arbitrary strings using the
Fred Drake81acc2e1998-04-04 06:35:41 +000012\method{update()} method, and at any point you can ask it for the
Guido van Rossum470be141995-03-17 16:07:09 +000013\dfn{digest} (a strong kind of 128-bit checksum,
14a.k.a. ``fingerprint'') of the contatenation of the strings fed to it
Fred Drake81acc2e1998-04-04 06:35:41 +000015so far using the \method{digest()} method.
16\index{checksum!MD5}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017
Fred Drake81acc2e1998-04-04 06:35:41 +000018For example, to obtain the digest of the string \code{'Nobody inspects
19the spammish repetition'}:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020
Fred Drake19479911998-02-13 06:58:54 +000021\begin{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000022>>> import md5
23>>> m = md5.new()
Guido van Rossum470be141995-03-17 16:07:09 +000024>>> m.update("Nobody inspects")
25>>> m.update(" the spammish repetition")
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026>>> m.digest()
Guido van Rossum470be141995-03-17 16:07:09 +000027'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Fred Drake19479911998-02-13 06:58:54 +000028\end{verbatim}
Fred Drake81acc2e1998-04-04 06:35:41 +000029
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030More condensed:
31
Fred Drake19479911998-02-13 06:58:54 +000032\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000033>>> md5.new("Nobody inspects the spammish repetition").digest()
34'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
Fred Drake19479911998-02-13 06:58:54 +000035\end{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000036
37\begin{funcdesc}{new}{\optional{arg}}
Guido van Rossum470be141995-03-17 16:07:09 +000038Return a new md5 object. If \var{arg} is present, the method call
39\code{update(\var{arg})} is made.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040\end{funcdesc}
41
Guido van Rossum31cce971995-01-04 19:17:34 +000042\begin{funcdesc}{md5}{\optional{arg}}
43For backward compatibility reasons, this is an alternative name for the
Fred Drake81acc2e1998-04-04 06:35:41 +000044\function{new()} function.
Guido van Rossum31cce971995-01-04 19:17:34 +000045\end{funcdesc}
46
Guido van Rossum470be141995-03-17 16:07:09 +000047An md5 object has the following methods:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048
Fred Drake81acc2e1998-04-04 06:35:41 +000049\begin{methoddesc}[md5]{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)}.
Fred Drake81acc2e1998-04-04 06:35:41 +000054\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055
Fred Drake81acc2e1998-04-04 06:35:41 +000056\begin{methoddesc}[md5]{digest}{}
57Return the digest of the strings passed to the \method{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.
Fred Drake81acc2e1998-04-04 06:35:41 +000060\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061
Fred Drake81acc2e1998-04-04 06:35:41 +000062\begin{methoddesc}[md5]{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.
Fred Drake81acc2e1998-04-04 06:35:41 +000066\end{methoddesc}