blob: 20d04abec9f712429c8ca87ca730199ffffa2c42 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`hashlib` --- Secure hashes and message digests
2====================================================
3
4.. module:: hashlib
5 :synopsis: Secure hash and message digest algorithms.
Benjamin Peterson058e31e2009-01-16 03:54:08 +00006.. moduleauthor:: Gregory P. Smith <greg@krypto.org>
7.. sectionauthor:: Gregory P. Smith <greg@krypto.org>
Georg Brandl116aa622007-08-15 14:28:22 +00008
9
Georg Brandl116aa622007-08-15 14:28:22 +000010.. index::
11 single: message digest, MD5
12 single: 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
Georg Brandl67ced422007-09-06 14:09:10 +000017algorithm (defined in Internet :rfc:`1321`). The terms "secure hash" and
18"message digest" are interchangeable. Older algorithms were called message
19digests. The modern term is secure hash.
Georg Brandl116aa622007-08-15 14:28:22 +000020
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000021.. note::
22 If you want the adler32 or crc32 hash functions they are available in
23 the :mod:`zlib` module.
24
Georg Brandl116aa622007-08-15 14:28:22 +000025.. warning::
26
27 Some algorithms have known hash collision weaknesses, see the FAQ at the end.
28
29There is one constructor method named for each type of :dfn:`hash`. All return
30a hash object with the same simple interface. For example: use :func:`sha1` to
Georg Brandl67ced422007-09-06 14:09:10 +000031create a SHA1 hash object. You can now feed this object with objects conforming
32to the buffer interface (normally :class:`bytes` objects) using the
33:meth:`update` method. At any point you can ask it for the :dfn:`digest` of the
34concatenation of the data fed to it so far using the :meth:`digest` or
35:meth:`hexdigest` methods.
36
37.. note::
38
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000039 For better multithreading performance, the Python GIL is released for
40 strings of more than 2047 bytes at object creation or on update.
41
42.. note::
43
Georg Brandl67ced422007-09-06 14:09:10 +000044 Feeding string objects is to :meth:`update` is not supported, as hashes work
45 on bytes, not on characters.
Georg Brandl116aa622007-08-15 14:28:22 +000046
Thomas Wouters1b7f8912007-09-19 03:06:30 +000047.. index:: single: OpenSSL; (use in module hashlib)
Georg Brandl116aa622007-08-15 14:28:22 +000048
49Constructors for hash algorithms that are always present in this module are
50:func:`md5`, :func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`, and
51:func:`sha512`. Additional algorithms may also be available depending upon the
52OpenSSL library that Python uses on your platform.
53
Georg Brandl67ced422007-09-06 14:09:10 +000054For example, to obtain the digest of the byte string ``b'Nobody inspects the
55spammish repetition'``::
Georg Brandl116aa622007-08-15 14:28:22 +000056
57 >>> import hashlib
58 >>> m = hashlib.md5()
Georg Brandl67ced422007-09-06 14:09:10 +000059 >>> m.update(b"Nobody inspects")
60 >>> m.update(b" the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +000061 >>> m.digest()
Georg Brandl67ced422007-09-06 14:09:10 +000062 b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
Guido van Rossuma19f80c2007-11-06 20:51:31 +000063 >>> m.digest_size
64 16
65 >>> m.block_size
66 64
Georg Brandl116aa622007-08-15 14:28:22 +000067
Christian Heimesfe337bf2008-03-23 21:54:12 +000068More condensed:
Georg Brandl116aa622007-08-15 14:28:22 +000069
Georg Brandl67ced422007-09-06 14:09:10 +000070 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
Benjamin Peterson0fa3f3d2008-12-29 20:52:09 +000071 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
Georg Brandl116aa622007-08-15 14:28:22 +000072
73A generic :func:`new` constructor that takes the string name of the desired
74algorithm as its first parameter also exists to allow access to the above listed
75hashes as well as any other algorithms that your OpenSSL library may offer. The
76named constructors are much faster than :func:`new` and should be preferred.
77
Christian Heimesfe337bf2008-03-23 21:54:12 +000078Using :func:`new` with an algorithm provided by OpenSSL:
Georg Brandl116aa622007-08-15 14:28:22 +000079
80 >>> h = hashlib.new('ripemd160')
Georg Brandl67ced422007-09-06 14:09:10 +000081 >>> h.update(b"Nobody inspects the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +000082 >>> h.hexdigest()
Benjamin Peterson0fa3f3d2008-12-29 20:52:09 +000083 'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
Georg Brandl116aa622007-08-15 14:28:22 +000084
85The following values are provided as constant attributes of the hash objects
86returned by the constructors:
87
88
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +000089.. data:: hash.digest_size
Georg Brandl116aa622007-08-15 14:28:22 +000090
Guido van Rossuma19f80c2007-11-06 20:51:31 +000091 The size of the resulting hash in bytes.
92
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +000093.. data:: hash.block_size
Guido van Rossuma19f80c2007-11-06 20:51:31 +000094
95 The internal block size of the hash algorithm in bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000096
97A hash object has the following methods:
98
99
100.. method:: hash.update(arg)
101
Georg Brandl67ced422007-09-06 14:09:10 +0000102 Update the hash object with the object *arg*, which must be interpretable as
103 a buffer of bytes. Repeated calls are equivalent to a single call with the
104 concatenation of all the arguments: ``m.update(a); m.update(b)`` is
105 equivalent to ``m.update(a+b)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Georg Brandl705d9d52009-05-05 09:29:50 +0000107 .. versionchanged:: 3.1
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000108
109 The Python GIL is released to allow other threads to run while
110 hash updates on data larger than 2048 bytes is taking place when
111 using hash algorithms supplied by OpenSSL.
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114.. method:: hash.digest()
115
Georg Brandl67ced422007-09-06 14:09:10 +0000116 Return the digest of the data passed to the :meth:`update` method so far.
117 This is a bytes array of size :attr:`digest_size` which may contain bytes in
118 the whole range from 0 to 255.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120
121.. method:: hash.hexdigest()
122
Georg Brandl67ced422007-09-06 14:09:10 +0000123 Like :meth:`digest` except the digest is returned as a string object of
124 double length, containing only hexadecimal digits. This may be used to
125 exchange the value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
128.. method:: hash.copy()
129
130 Return a copy ("clone") of the hash object. This can be used to efficiently
Georg Brandl67ced422007-09-06 14:09:10 +0000131 compute the digests of data sharing a common initial substring.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133
134.. seealso::
135
136 Module :mod:`hmac`
137 A module to generate message authentication codes using hashes.
138
139 Module :mod:`base64`
140 Another way to encode binary hashes for non-binary environments.
141
142 http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
143 The FIPS 180-2 publication on Secure Hash Algorithms.
144
Georg Brandl8c5a7bb2010-05-21 20:36:03 +0000145 http://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms
146 Wikipedia article with information on which algorithms have known issues and
Georg Brandl116aa622007-08-15 14:28:22 +0000147 what that means regarding their use.
148