blob: bc8ab2ca6d32a230d8aa4ed95fe1b2332805fb76 [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
Raymond Hettinger469271d2011-01-27 20:38:46 +000014**Source code:** :source:`Lib/hashlib.py`
15
16--------------
17
Georg Brandl116aa622007-08-15 14:28:22 +000018This module implements a common interface to many different secure hash and
19message digest algorithms. Included are the FIPS secure hash algorithms SHA1,
20SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA's MD5
Georg Brandl67ced422007-09-06 14:09:10 +000021algorithm (defined in Internet :rfc:`1321`). The terms "secure hash" and
22"message digest" are interchangeable. Older algorithms were called message
23digests. The modern term is secure hash.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000025.. note::
26 If you want the adler32 or crc32 hash functions they are available in
27 the :mod:`zlib` module.
28
Georg Brandl116aa622007-08-15 14:28:22 +000029.. warning::
30
31 Some algorithms have known hash collision weaknesses, see the FAQ at the end.
32
33There is one constructor method named for each type of :dfn:`hash`. All return
34a hash object with the same simple interface. For example: use :func:`sha1` to
Georg Brandl67ced422007-09-06 14:09:10 +000035create a SHA1 hash object. You can now feed this object with objects conforming
36to the buffer interface (normally :class:`bytes` objects) using the
37:meth:`update` method. At any point you can ask it for the :dfn:`digest` of the
38concatenation of the data fed to it so far using the :meth:`digest` or
39:meth:`hexdigest` methods.
40
41.. note::
42
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000043 For better multithreading performance, the Python GIL is released for
44 strings of more than 2047 bytes at object creation or on update.
45
46.. note::
47
Georg Brandl67ced422007-09-06 14:09:10 +000048 Feeding string objects is to :meth:`update` is not supported, as hashes work
49 on bytes, not on characters.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Thomas Wouters1b7f8912007-09-19 03:06:30 +000051.. index:: single: OpenSSL; (use in module hashlib)
Georg Brandl116aa622007-08-15 14:28:22 +000052
53Constructors for hash algorithms that are always present in this module are
54:func:`md5`, :func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`, and
55:func:`sha512`. Additional algorithms may also be available depending upon the
56OpenSSL library that Python uses on your platform.
57
Georg Brandl67ced422007-09-06 14:09:10 +000058For example, to obtain the digest of the byte string ``b'Nobody inspects the
59spammish repetition'``::
Georg Brandl116aa622007-08-15 14:28:22 +000060
61 >>> import hashlib
62 >>> m = hashlib.md5()
Georg Brandl67ced422007-09-06 14:09:10 +000063 >>> m.update(b"Nobody inspects")
64 >>> m.update(b" the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +000065 >>> m.digest()
Georg Brandl67ced422007-09-06 14:09:10 +000066 b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
Guido van Rossuma19f80c2007-11-06 20:51:31 +000067 >>> m.digest_size
68 16
69 >>> m.block_size
70 64
Georg Brandl116aa622007-08-15 14:28:22 +000071
Christian Heimesfe337bf2008-03-23 21:54:12 +000072More condensed:
Georg Brandl116aa622007-08-15 14:28:22 +000073
Georg Brandl67ced422007-09-06 14:09:10 +000074 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
Benjamin Peterson0fa3f3d2008-12-29 20:52:09 +000075 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
Georg Brandl116aa622007-08-15 14:28:22 +000076
Gregory P. Smith13b55292010-09-06 08:30:23 +000077.. function:: new(name[, data])
78
79 Is a generic constructor that takes the string name of the desired
80 algorithm as its first parameter. It also exists to allow access to the
81 above listed hashes as well as any other algorithms that your OpenSSL
82 library may offer. The named constructors are much faster than :func:`new`
83 and should be preferred.
Georg Brandl116aa622007-08-15 14:28:22 +000084
Christian Heimesfe337bf2008-03-23 21:54:12 +000085Using :func:`new` with an algorithm provided by OpenSSL:
Georg Brandl116aa622007-08-15 14:28:22 +000086
87 >>> h = hashlib.new('ripemd160')
Georg Brandl67ced422007-09-06 14:09:10 +000088 >>> h.update(b"Nobody inspects the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +000089 >>> h.hexdigest()
Benjamin Peterson0fa3f3d2008-12-29 20:52:09 +000090 'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
Georg Brandl116aa622007-08-15 14:28:22 +000091
Gregory P. Smith13b55292010-09-06 08:30:23 +000092Hashlib provides the following constant attributes:
Gregory P. Smith86508cc2010-03-01 02:05:26 +000093
Gregory P. Smith13b55292010-09-06 08:30:23 +000094.. data:: algorithms_guaranteed
Gregory P. Smith86508cc2010-03-01 02:05:26 +000095
Gregory P. Smith13b55292010-09-06 08:30:23 +000096 Contains the names of the hash algorithms guaranteed to be supported
97 by this module on all platforms.
98
99 .. versionadded:: 3.2
100
101.. data:: algorithms_available
102
103 Contains the names of the hash algorithms that are available
104 in the running Python interpreter. These names will be recognized
105 when passed to :func:`new`. :attr:`algorithms_guaranteed`
106 will always be a subset. Duplicate algorithms with different
107 name formats may appear in this set (thanks to OpenSSL).
Gregory P. Smith86508cc2010-03-01 02:05:26 +0000108
109 .. versionadded:: 3.2
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111The following values are provided as constant attributes of the hash objects
112returned by the constructors:
113
114
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000115.. data:: hash.digest_size
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000117 The size of the resulting hash in bytes.
118
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000119.. data:: hash.block_size
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000120
121 The internal block size of the hash algorithm in bytes.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123A hash object has the following methods:
124
125
126.. method:: hash.update(arg)
127
Georg Brandl67ced422007-09-06 14:09:10 +0000128 Update the hash object with the object *arg*, which must be interpretable as
129 a buffer of bytes. Repeated calls are equivalent to a single call with the
130 concatenation of all the arguments: ``m.update(a); m.update(b)`` is
131 equivalent to ``m.update(a+b)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Georg Brandl705d9d52009-05-05 09:29:50 +0000133 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000134 The Python GIL is released to allow other threads to run while hash
135 updates on data larger than 2048 bytes is taking place when using hash
136 algorithms supplied by OpenSSL.
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000137
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139.. method:: hash.digest()
140
Georg Brandl67ced422007-09-06 14:09:10 +0000141 Return the digest of the data passed to the :meth:`update` method so far.
Senthil Kumaran627284c2010-12-30 07:07:58 +0000142 This is a bytes object of size :attr:`digest_size` which may contain bytes in
Georg Brandl67ced422007-09-06 14:09:10 +0000143 the whole range from 0 to 255.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
146.. method:: hash.hexdigest()
147
Georg Brandl67ced422007-09-06 14:09:10 +0000148 Like :meth:`digest` except the digest is returned as a string object of
149 double length, containing only hexadecimal digits. This may be used to
150 exchange the value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152
153.. method:: hash.copy()
154
155 Return a copy ("clone") of the hash object. This can be used to efficiently
Georg Brandl67ced422007-09-06 14:09:10 +0000156 compute the digests of data sharing a common initial substring.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
159.. seealso::
160
161 Module :mod:`hmac`
162 A module to generate message authentication codes using hashes.
163
164 Module :mod:`base64`
165 Another way to encode binary hashes for non-binary environments.
166
167 http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
168 The FIPS 180-2 publication on Secure Hash Algorithms.
169
Georg Brandlfd0eb3f2010-05-21 20:28:13 +0000170 http://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms
171 Wikipedia article with information on which algorithms have known issues and
Georg Brandl116aa622007-08-15 14:28:22 +0000172 what that means regarding their use.
173