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