blob: 38105ae871daae30f42bfa436de7be5d556f921f [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
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00007\deprecated{2.5}{Use the \refmodule{hashlib} module instead.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008
9This module implements the interface to RSA's MD5 message digest
Fred Drake81acc2e1998-04-04 06:35:41 +000010\index{message digest, MD5}
Fred Drakec5891241998-02-09 19:16:20 +000011algorithm (see also Internet \rfc{1321}). Its use is quite
Tim Peters1de80982000-09-18 15:34:57 +000012straightforward:\ use \function{new()} to create an md5 object.
Guido van Rossum470be141995-03-17 16:07:09 +000013You can now feed this object with arbitrary strings using the
Fred Drake81acc2e1998-04-04 06:35:41 +000014\method{update()} method, and at any point you can ask it for the
Guido van Rossum470be141995-03-17 16:07:09 +000015\dfn{digest} (a strong kind of 128-bit checksum,
Thomas Woutersf8316632000-07-16 19:01:10 +000016a.k.a. ``fingerprint'') of the concatenation of the strings fed to it
Fred Drake81acc2e1998-04-04 06:35:41 +000017so far using the \method{digest()} method.
18\index{checksum!MD5}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000019
Fred Drake81acc2e1998-04-04 06:35:41 +000020For example, to obtain the digest of the string \code{'Nobody inspects
21the spammish repetition'}:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022
Fred Drake19479911998-02-13 06:58:54 +000023\begin{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000024>>> import md5
25>>> m = md5.new()
Guido van Rossum470be141995-03-17 16:07:09 +000026>>> m.update("Nobody inspects")
27>>> m.update(" the spammish repetition")
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028>>> m.digest()
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +000029'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
Fred Drake19479911998-02-13 06:58:54 +000030\end{verbatim}
Fred Drake81acc2e1998-04-04 06:35:41 +000031
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032More condensed:
33
Fred Drake19479911998-02-13 06:58:54 +000034\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000035>>> md5.new("Nobody inspects the spammish repetition").digest()
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +000036'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
Fred Drake19479911998-02-13 06:58:54 +000037\end{verbatim}
Guido van Rossum31cce971995-01-04 19:17:34 +000038
Andrew M. Kuchlingbc4a1c22001-11-02 21:44:09 +000039The following values are provided as constants in the module and as
40attributes of the md5 objects returned by \function{new()}:
41
42\begin{datadesc}{digest_size}
43 The size of the resulting digest in bytes. This is always
44 \code{16}.
45\end{datadesc}
46
Georg Brandl5e8d8f92005-12-22 16:15:00 +000047The md5 module provides the following functions:
Andrew M. Kuchlingbc4a1c22001-11-02 21:44:09 +000048
Guido van Rossum31cce971995-01-04 19:17:34 +000049\begin{funcdesc}{new}{\optional{arg}}
Guido van Rossum470be141995-03-17 16:07:09 +000050Return a new md5 object. If \var{arg} is present, the method call
51\code{update(\var{arg})} is made.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052\end{funcdesc}
53
Guido van Rossum31cce971995-01-04 19:17:34 +000054\begin{funcdesc}{md5}{\optional{arg}}
55For backward compatibility reasons, this is an alternative name for the
Fred Drake81acc2e1998-04-04 06:35:41 +000056\function{new()} function.
Guido van Rossum31cce971995-01-04 19:17:34 +000057\end{funcdesc}
58
Guido van Rossum470be141995-03-17 16:07:09 +000059An md5 object has the following methods:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000060
Fred Drake81acc2e1998-04-04 06:35:41 +000061\begin{methoddesc}[md5]{update}{arg}
Guido van Rossum470be141995-03-17 16:07:09 +000062Update the md5 object with the string \var{arg}. Repeated calls are
63equivalent to a single call with the concatenation of all the
Fred Drake91f2f262001-07-06 19:28:48 +000064arguments: \code{m.update(a); m.update(b)} is equivalent to
Guido van Rossum470be141995-03-17 16:07:09 +000065\code{m.update(a+b)}.
Fred Drake81acc2e1998-04-04 06:35:41 +000066\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067
Fred Drake81acc2e1998-04-04 06:35:41 +000068\begin{methoddesc}[md5]{digest}{}
69Return the digest of the strings passed to the \method{update()}
Tim Peters1de80982000-09-18 15:34:57 +000070method so far. This is a 16-byte string which may contain
Guido van Rossum470be141995-03-17 16:07:09 +000071non-\ASCII{} characters, including null bytes.
Fred Drake81acc2e1998-04-04 06:35:41 +000072\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073
Barry Warsaw4ef4be52000-08-15 06:00:28 +000074\begin{methoddesc}[md5]{hexdigest}{}
75Like \method{digest()} except the digest is returned as a string of
Tim Peters1de80982000-09-18 15:34:57 +000076length 32, containing only hexadecimal digits. This may
77be used to exchange the value safely in email or other non-binary
78environments.
Barry Warsaw4ef4be52000-08-15 06:00:28 +000079\end{methoddesc}
80
Fred Drake81acc2e1998-04-04 06:35:41 +000081\begin{methoddesc}[md5]{copy}{}
Guido van Rossum470be141995-03-17 16:07:09 +000082Return a copy (``clone'') of the md5 object. This can be used to
83efficiently compute the digests of strings that share a common initial
84substring.
Fred Drake81acc2e1998-04-04 06:35:41 +000085\end{methoddesc}
Fred Drake4dfad572000-09-14 21:47:32 +000086
87
88\begin{seealso}
89 \seemodule{sha}{Similar module implementing the Secure Hash
90 Algorithm (SHA). The SHA algorithm is considered a
91 more secure hash.}
92\end{seealso}