blob: 6f837b4e89a6adbd886bb81029a1f5071a5949ad [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
Tim Peters1de80982000-09-18 15:34:57 +000011straightforward:\ use \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,
Thomas Woutersf8316632000-07-16 19:01:10 +000015a.k.a. ``fingerprint'') of the concatenation 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()
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +000028'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
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()
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +000035'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
Fred Drake19479911998-02-13 06:58:54 +000036\end{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000037
Andrew M. Kuchlingbc4a1c22001-11-02 21:44:09 +000038The following values are provided as constants in the module and as
39attributes of the md5 objects returned by \function{new()}:
40
41\begin{datadesc}{digest_size}
42 The size of the resulting digest in bytes. This is always
43 \code{16}.
44\end{datadesc}
45
46md5 objects support the following methods:
47
Guido van Rossum31cce971995-01-04 19:17:34 +000048\begin{funcdesc}{new}{\optional{arg}}
Guido van Rossum470be141995-03-17 16:07:09 +000049Return a new md5 object. If \var{arg} is present, the method call
50\code{update(\var{arg})} is made.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051\end{funcdesc}
52
Guido van Rossum31cce971995-01-04 19:17:34 +000053\begin{funcdesc}{md5}{\optional{arg}}
54For backward compatibility reasons, this is an alternative name for the
Fred Drake81acc2e1998-04-04 06:35:41 +000055\function{new()} function.
Guido van Rossum31cce971995-01-04 19:17:34 +000056\end{funcdesc}
57
Guido van Rossum470be141995-03-17 16:07:09 +000058An md5 object has the following methods:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059
Fred Drake81acc2e1998-04-04 06:35:41 +000060\begin{methoddesc}[md5]{update}{arg}
Guido van Rossum470be141995-03-17 16:07:09 +000061Update the md5 object with the string \var{arg}. Repeated calls are
62equivalent to a single call with the concatenation of all the
Fred Drake91f2f262001-07-06 19:28:48 +000063arguments: \code{m.update(a); m.update(b)} is equivalent to
Guido van Rossum470be141995-03-17 16:07:09 +000064\code{m.update(a+b)}.
Fred Drake81acc2e1998-04-04 06:35:41 +000065\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066
Fred Drake81acc2e1998-04-04 06:35:41 +000067\begin{methoddesc}[md5]{digest}{}
68Return the digest of the strings passed to the \method{update()}
Tim Peters1de80982000-09-18 15:34:57 +000069method so far. This is a 16-byte string which may contain
Guido van Rossum470be141995-03-17 16:07:09 +000070non-\ASCII{} characters, including null bytes.
Fred Drake81acc2e1998-04-04 06:35:41 +000071\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072
Barry Warsaw4ef4be52000-08-15 06:00:28 +000073\begin{methoddesc}[md5]{hexdigest}{}
74Like \method{digest()} except the digest is returned as a string of
Tim Peters1de80982000-09-18 15:34:57 +000075length 32, containing only hexadecimal digits. This may
76be used to exchange the value safely in email or other non-binary
77environments.
Barry Warsaw4ef4be52000-08-15 06:00:28 +000078\end{methoddesc}
79
Fred Drake81acc2e1998-04-04 06:35:41 +000080\begin{methoddesc}[md5]{copy}{}
Guido van Rossum470be141995-03-17 16:07:09 +000081Return a copy (``clone'') of the md5 object. This can be used to
82efficiently compute the digests of strings that share a common initial
83substring.
Fred Drake81acc2e1998-04-04 06:35:41 +000084\end{methoddesc}
Fred Drake4dfad572000-09-14 21:47:32 +000085
86
87\begin{seealso}
88 \seemodule{sha}{Similar module implementing the Secure Hash
89 Algorithm (SHA). The SHA algorithm is considered a
90 more secure hash.}
91\end{seealso}