~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Doc/library/hashlib.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

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