blob: 17f5179309f9312f8e07bb15fd997f7ac115a966 [file] [log] [blame]
Gregory P. Smith7208af42005-08-22 18:31:41 +00001\section{\module{hashlib} ---
2 Secure hashes and message digests}
3
4\declaremodule{builtin}{hashlib}
5\modulesynopsis{Secure hash and message digest algorithms.}
6\moduleauthor{Gregory P. Smith}{greg@users.sourceforge.net}
7\sectionauthor{Gregory P. Smith}{greg@users.sourceforge.net}
8
9\versionadded{2.5}
10
11\index{message digest, MD5}
12\index{secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512}
13
14This module implements a common interface to many different secure hash and
15message digest algorithms. Included are the FIPS secure hash algorithms SHA1,
16SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA's MD5
17algorithm (defined in Internet \rfc{1321}).
Georg Brandlcaa94bd2006-01-23 22:00:17 +000018The terms secure hash and message digest are interchangeable. Older
Gregory P. Smith7208af42005-08-22 18:31:41 +000019algorithms were called message digests. The modern term is secure hash.
20
21\warning{Some algorithms have known hash collision weaknesses, see the FAQ at the end.}
22
23There is one constructor method named for each type of \dfn{hash}. All return
24a hash object with the same simple interface.
25For example: use \function{sha1()} to create a SHA1 hash object.
26You can now feed this object with arbitrary strings using the \method{update()}
27method. At any point you can ask it for the \dfn{digest} of the concatenation
28of the strings fed to it so far using the \method{digest()} or
29\method{hexdigest()} methods.
30
31Constructors for hash algorithms that are always present in this module are
32\function{md5()}, \function{sha1()}, \function{sha224()}, \function{sha256()},
33\function{sha384()}, and \function{sha512()}. Additional algorithms may also
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034be available depending upon the OpenSSL library that Python uses on your platform.
Gregory P. Smith7208af42005-08-22 18:31:41 +000035\index{OpenSSL}
36
37For example, to obtain the digest of the string \code{'Nobody inspects
38the spammish repetition'}:
39
40\begin{verbatim}
41>>> import hashlib
42>>> m = hashlib.md5()
43>>> m.update("Nobody inspects")
44>>> m.update(" the spammish repetition")
45>>> m.digest()
46'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
47\end{verbatim}
48
49More condensed:
50
51\begin{verbatim}
52>>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()
53'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
54\end{verbatim}
55
56A generic \function{new()} constructor that takes the string name of the
57desired algorithm as its first parameter also exists to allow access to the
58above listed hashes as well as any other algorithms that your OpenSSL library
59may offer. The named constructors are much faster than \function{new()} and
60should be preferred.
61
62Using \function{new()} with an algorithm provided by OpenSSL:
63
64\begin{verbatim}
65>>> h = hashlib.new('ripemd160')
66>>> h.update("Nobody inspects the spammish repetition")
67>>> h.hexdigest()
68'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
69\end{verbatim}
70
71The following values are provided as constant attributes of the hash objects
72returned by the constructors:
73
74\begin{datadesc}{digest_size}
75 The size of the resulting digest in bytes.
76\end{datadesc}
77
78A hash object has the following methods:
79
80\begin{methoddesc}[hash]{update}{arg}
81Update the hash object with the string \var{arg}. Repeated calls are
82equivalent to a single call with the concatenation of all the
83arguments: \code{m.update(a); m.update(b)} is equivalent to
84\code{m.update(a+b)}.
85\end{methoddesc}
86
87\begin{methoddesc}[hash]{digest}{}
88Return the digest of the strings passed to the \method{update()}
Thomas Wouters89f507f2006-12-13 04:49:30 +000089method so far. This is a string of \member{digest_size} bytes which may
90contain non-\ASCII{} characters, including null bytes.
Gregory P. Smith7208af42005-08-22 18:31:41 +000091\end{methoddesc}
92
93\begin{methoddesc}[hash]{hexdigest}{}
94Like \method{digest()} except the digest is returned as a string of
95double length, containing only hexadecimal digits. This may
96be used to exchange the value safely in email or other non-binary
97environments.
98\end{methoddesc}
99
100\begin{methoddesc}[hash]{copy}{}
101Return a copy (``clone'') of the hash object. This can be used to
102efficiently compute the digests of strings that share a common initial
103substring.
104\end{methoddesc}
105
106\begin{seealso}
107 \seemodule{hmac}{A module to generate message authentication codes using hashes.}
108 \seemodule{base64}{Another way to encode binary hashes for non-binary environments.}
109 \seeurl{http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf}
110 {The FIPS 180-2 publication on Secure Hash Algorithms.}
111 \seeurl{http://www.cryptography.com/cnews/hash.html}
112 {Hash Collision FAQ with information on which algorithms have known issues and
113 what that means regarding their use.}
114\end{seealso}