blob: 702b2bb54b5df3f36ec999ea204a202022ba1165 [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
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
Fred Drake91f2f262001-07-06 19:28:48 +000053arguments: \code{m.update(a); m.update(b)} is equivalent to
Guido van Rossum470be141995-03-17 16:07:09 +000054\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()}
Tim Peters1de80982000-09-18 15:34:57 +000059method so far. This is a 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
Barry Warsaw4ef4be52000-08-15 06:00:28 +000063\begin{methoddesc}[md5]{hexdigest}{}
64Like \method{digest()} except the digest is returned as a string of
Tim Peters1de80982000-09-18 15:34:57 +000065length 32, containing only hexadecimal digits. This may
66be used to exchange the value safely in email or other non-binary
67environments.
Barry Warsaw4ef4be52000-08-15 06:00:28 +000068\end{methoddesc}
69
Fred Drake81acc2e1998-04-04 06:35:41 +000070\begin{methoddesc}[md5]{copy}{}
Guido van Rossum470be141995-03-17 16:07:09 +000071Return a copy (``clone'') of the md5 object. This can be used to
72efficiently compute the digests of strings that share a common initial
73substring.
Fred Drake81acc2e1998-04-04 06:35:41 +000074\end{methoddesc}
Fred Drake4dfad572000-09-14 21:47:32 +000075
76
77\begin{seealso}
78 \seemodule{sha}{Similar module implementing the Secure Hash
79 Algorithm (SHA). The SHA algorithm is considered a
80 more secure hash.}
81\end{seealso}