blob: 2741265d1c35c12361c38de7fb0fc4316a4ff3b5 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`hashlib` --- Secure hashes and message digests
3====================================================
4
5.. module:: hashlib
6 :synopsis: Secure hash and message digest algorithms.
7.. moduleauthor:: Gregory P. Smith <greg@users.sourceforge.net>
8.. sectionauthor:: Gregory P. Smith <greg@users.sourceforge.net>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011.. index::
12 single: message digest, MD5
13 single: secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512
14
15This module implements a common interface to many different secure hash and
16message digest algorithms. Included are the FIPS secure hash algorithms SHA1,
17SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA's MD5
Georg Brandl67ced422007-09-06 14:09:10 +000018algorithm (defined in Internet :rfc:`1321`). The terms "secure hash" and
19"message digest" are interchangeable. Older algorithms were called message
20digests. The modern term is secure hash.
Georg Brandl116aa622007-08-15 14:28:22 +000021
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000022.. note::
23 If you want the adler32 or crc32 hash functions they are available in
24 the :mod:`zlib` module.
25
Georg Brandl116aa622007-08-15 14:28:22 +000026.. warning::
27
28 Some algorithms have known hash collision weaknesses, see the FAQ at the end.
29
30There is one constructor method named for each type of :dfn:`hash`. All return
31a hash object with the same simple interface. For example: use :func:`sha1` to
Georg Brandl67ced422007-09-06 14:09:10 +000032create a SHA1 hash object. You can now feed this object with objects conforming
33to the buffer interface (normally :class:`bytes` objects) using the
34:meth:`update` method. At any point you can ask it for the :dfn:`digest` of the
35concatenation of the data fed to it so far using the :meth:`digest` or
36:meth:`hexdigest` methods.
37
38.. note::
39
40 Feeding string objects is to :meth:`update` is not supported, as hashes work
41 on bytes, not on characters.
Georg Brandl116aa622007-08-15 14:28:22 +000042
Thomas Wouters1b7f8912007-09-19 03:06:30 +000043.. index:: single: OpenSSL; (use in module hashlib)
Georg Brandl116aa622007-08-15 14:28:22 +000044
45Constructors for hash algorithms that are always present in this module are
46:func:`md5`, :func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`, and
47:func:`sha512`. Additional algorithms may also be available depending upon the
48OpenSSL library that Python uses on your platform.
49
Georg Brandl67ced422007-09-06 14:09:10 +000050For example, to obtain the digest of the byte string ``b'Nobody inspects the
51spammish repetition'``::
Georg Brandl116aa622007-08-15 14:28:22 +000052
53 >>> import hashlib
54 >>> m = hashlib.md5()
Georg Brandl67ced422007-09-06 14:09:10 +000055 >>> m.update(b"Nobody inspects")
56 >>> m.update(b" the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +000057 >>> m.digest()
Georg Brandl67ced422007-09-06 14:09:10 +000058 b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
Guido van Rossuma19f80c2007-11-06 20:51:31 +000059 >>> m.digest_size
60 16
61 >>> m.block_size
62 64
Georg Brandl116aa622007-08-15 14:28:22 +000063
Christian Heimesfe337bf2008-03-23 21:54:12 +000064More condensed:
Georg Brandl116aa622007-08-15 14:28:22 +000065
Georg Brandl67ced422007-09-06 14:09:10 +000066 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
67 b'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
Georg Brandl116aa622007-08-15 14:28:22 +000068
69A generic :func:`new` constructor that takes the string name of the desired
70algorithm as its first parameter also exists to allow access to the above listed
71hashes as well as any other algorithms that your OpenSSL library may offer. The
72named constructors are much faster than :func:`new` and should be preferred.
73
Christian Heimesfe337bf2008-03-23 21:54:12 +000074Using :func:`new` with an algorithm provided by OpenSSL:
Georg Brandl116aa622007-08-15 14:28:22 +000075
76 >>> h = hashlib.new('ripemd160')
Georg Brandl67ced422007-09-06 14:09:10 +000077 >>> h.update(b"Nobody inspects the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +000078 >>> h.hexdigest()
Georg Brandl67ced422007-09-06 14:09:10 +000079 b'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
Georg Brandl116aa622007-08-15 14:28:22 +000080
81The following values are provided as constant attributes of the hash objects
82returned by the constructors:
83
84
85.. data:: digest_size
86
Guido van Rossuma19f80c2007-11-06 20:51:31 +000087 The size of the resulting hash in bytes.
88
89.. data:: block_size
90
91 The internal block size of the hash algorithm in bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93A hash object has the following methods:
94
95
96.. method:: hash.update(arg)
97
Georg Brandl67ced422007-09-06 14:09:10 +000098 Update the hash object with the object *arg*, which must be interpretable as
99 a buffer of bytes. Repeated calls are equivalent to a single call with the
100 concatenation of all the arguments: ``m.update(a); m.update(b)`` is
101 equivalent to ``m.update(a+b)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103
104.. method:: hash.digest()
105
Georg Brandl67ced422007-09-06 14:09:10 +0000106 Return the digest of the data passed to the :meth:`update` method so far.
107 This is a bytes array of size :attr:`digest_size` which may contain bytes in
108 the whole range from 0 to 255.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
111.. method:: hash.hexdigest()
112
Georg Brandl67ced422007-09-06 14:09:10 +0000113 Like :meth:`digest` except the digest is returned as a string object of
114 double length, containing only hexadecimal digits. This may be used to
115 exchange the value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
118.. method:: hash.copy()
119
120 Return a copy ("clone") of the hash object. This can be used to efficiently
Georg Brandl67ced422007-09-06 14:09:10 +0000121 compute the digests of data sharing a common initial substring.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
124.. seealso::
125
126 Module :mod:`hmac`
127 A module to generate message authentication codes using hashes.
128
129 Module :mod:`base64`
130 Another way to encode binary hashes for non-binary environments.
131
132 http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
133 The FIPS 180-2 publication on Secure Hash Algorithms.
134
135 http://www.cryptography.com/cnews/hash.html
136 Hash Collision FAQ with information on which algorithms have known issues and
137 what that means regarding their use.
138