blob: b7eee2f58a319963dbf1a7e083d87ba08cb147e9 [file] [log] [blame]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07001.. hazmat::
Donald Stufftd8f01182013-10-27 16:59:56 -04002
Donald Stuffte51fb932013-10-27 17:26:17 -04003Message Digests
4===============
5
Donald Stufftf04317a2013-10-27 16:44:30 -04006.. currentmodule:: cryptography.hazmat.primitives.hashes
David Reid1f3d7182013-10-22 16:55:18 -07007
David Reidef0fcf22013-11-06 11:12:45 -08008.. class:: Hash(algorithm, backend)
Matthew Iversen505491b2013-10-19 15:56:17 +11009
David Reid55602982013-11-01 13:34:05 -070010 A cryptographic hash function takes an arbitrary block of data and
11 calculates a fixed-size bit string (a digest), such that different data
12 results (with a high probability) in different digests.
Matthew Iversen505491b2013-10-19 15:56:17 +110013
David Reid55602982013-11-01 13:34:05 -070014 This is an implementation of
Alex Gaynorab5f0112013-11-08 10:34:00 -080015 :class:`~cryptography.hazmat.primitives.interfaces.HashContext` meant to
David Reid55602982013-11-01 13:34:05 -070016 be used with
Alex Gaynorab5f0112013-11-08 10:34:00 -080017 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
David Reid55602982013-11-01 13:34:05 -070018 implementations to provide an incremental interface to calculating
19 various message digests.
Alex Gaynor23d01a22013-10-28 10:14:46 -070020
21 .. doctest::
David Reid846460a2013-11-06 11:24:50 -080022
Alex Gaynorf8796b12013-12-13 20:28:55 -080023 >>> from cryptography.hazmat.backends import default_backend
Alex Gaynor23d01a22013-10-28 10:14:46 -070024 >>> from cryptography.hazmat.primitives import hashes
David Reid63fa19a2013-11-20 10:49:13 -080025 >>> digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
Alex Gaynor23d01a22013-10-28 10:14:46 -070026 >>> digest.update(b"abc")
27 >>> digest.update(b"123")
David Reid30b16132013-10-31 13:37:24 -070028 >>> digest.finalize()
29 'l\xa1=R\xcap\xc8\x83\xe0\xf0\xbb\x10\x1eBZ\x89\xe8bM\xe5\x1d\xb2\xd29%\x93\xafj\x84\x11\x80\x90'
Alex Gaynorf3b06cd2013-10-21 21:49:50 -070030
Alex Stapleton1b1327c2013-12-21 15:16:57 +000031 If the backend doesn't support the requested ``algorithm`` an
Alex Stapleton79043462014-03-09 16:46:26 +000032 :class:`~cryptography.exceptions.UnsupportedHash` exception will be raised.
Alex Stapleton1b1327c2013-12-21 15:16:57 +000033
Alex Gaynor94801292013-11-13 10:33:01 -080034 Keep in mind that attacks against cryptographic hashes only get stronger
35 with time, and that often algorithms that were once thought to be strong,
36 become broken. Because of this it's important to include a plan for
37 upgrading the hash algorithm you use over time. For more information, see
38 `Lifetimes of cryptographic hash functions`_.
39
David Reid663295d2013-11-20 13:55:08 -080040 :param algorithm: A
41 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
42 provider such as those described in
43 :ref:`below <cryptographic-hash-algorithms>`.
44 :param backend: A
Alex Gaynorf8796b12013-12-13 20:28:55 -080045 :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
David Reid663295d2013-11-20 13:55:08 -080046 provider.
47
Ayrxb482ca12014-03-16 13:06:25 +080048 :raises cryptography.exceptions.UnsupportedInterface: This is raised if the
49 provided ``backend`` does not implement
50 :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
51
Paul Kehrer6b9ddeb2013-10-19 12:28:15 -050052 .. method:: update(data)
Matthew Iversen505491b2013-10-19 15:56:17 +110053
Alex Stapleton79043462014-03-09 16:46:26 +000054 :param bytes data: The bytes to be hashed.
55 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`.
Matthew Iversen505491b2013-10-19 15:56:17 +110056
57 .. method:: copy()
58
Alex Stapleton79043462014-03-09 16:46:26 +000059 Copy this :class:`Hash` instance, usually so that you may call
60 :meth:`finalize` to get an intermediate digest value while we continue
61 to call :meth:`update` on the original instance.
David Reid6392a9c2013-11-13 10:01:15 -080062
Alex Stapleton63b3de22014-02-08 09:43:16 +000063 :return: A new instance of :class:`Hash` that can be updated
Alex Stapleton79043462014-03-09 16:46:26 +000064 and finalized independently of the original instance.
65 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`.
Matthew Iversen505491b2013-10-19 15:56:17 +110066
David Reid30b16132013-10-31 13:37:24 -070067 .. method:: finalize()
Alex Gaynor14968452013-11-01 14:05:14 -070068
David Reid55602982013-11-01 13:34:05 -070069 Finalize the current context and return the message digest as bytes.
70
Alex Stapleton79043462014-03-09 16:46:26 +000071 After ``finalize`` has been called this object can no longer be used
72 and :meth:`update`, :meth:`copy`, and :meth:`finalize` will raise an
73 :class:`~cryptography.exceptions.AlreadyFinalized` exception.
Matthew Iversen505491b2013-10-19 15:56:17 +110074
75 :return bytes: The message digest as bytes.
76
Matthew Iversen505491b2013-10-19 15:56:17 +110077
David Reid663295d2013-11-20 13:55:08 -080078.. _cryptographic-hash-algorithms:
79
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050080SHA-1
Matthew Iversen505491b2013-10-19 15:56:17 +110081~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050082
83.. attention::
84
85 NIST has deprecated SHA-1 in favor of the SHA-2 variants. New applications
86 are strongly suggested to use SHA-2 over SHA-1.
87
David Reid1f3d7182013-10-22 16:55:18 -070088.. class:: SHA1()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050089
Alex Stapleton79043462014-03-09 16:46:26 +000090 SHA-1 is a cryptographic hash function standardized by NIST. It produces an
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050091 160-bit message digest.
92
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050093SHA-2 Family
Matthew Iversen505491b2013-10-19 15:56:17 +110094~~~~~~~~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050095
David Reid1f3d7182013-10-22 16:55:18 -070096.. class:: SHA224()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050097
Alex Stapleton79043462014-03-09 16:46:26 +000098 SHA-224 is a cryptographic hash function from the SHA-2 family and is
99 standardized by NIST. It produces a 224-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500100
David Reid1f3d7182013-10-22 16:55:18 -0700101.. class:: SHA256()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500102
Alex Stapleton79043462014-03-09 16:46:26 +0000103 SHA-256 is a cryptographic hash function from the SHA-2 family and is
104 standardized by NIST. It produces a 256-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500105
David Reid1f3d7182013-10-22 16:55:18 -0700106.. class:: SHA384()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500107
Alex Stapleton79043462014-03-09 16:46:26 +0000108 SHA-384 is a cryptographic hash function from the SHA-2 family and is
109 standardized by NIST. It produces a 384-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500110
David Reid1f3d7182013-10-22 16:55:18 -0700111.. class:: SHA512()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500112
Alex Stapleton79043462014-03-09 16:46:26 +0000113 SHA-512 is a cryptographic hash function from the SHA-2 family and is
114 standardized by NIST. It produces a 512-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500115
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500116RIPEMD160
Matthew Iversen505491b2013-10-19 15:56:17 +1100117~~~~~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500118
David Reid1f3d7182013-10-22 16:55:18 -0700119.. class:: RIPEMD160()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500120
121 RIPEMD160 is a cryptographic hash function that is part of ISO/IEC
Alex Stapleton79043462014-03-09 16:46:26 +0000122 10118-3:2004. It produces a 160-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500123
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500124Whirlpool
Matthew Iversen505491b2013-10-19 15:56:17 +1100125~~~~~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500126
David Reid1f3d7182013-10-22 16:55:18 -0700127.. class:: Whirlpool()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500128
129 Whirlpool is a cryptographic hash function that is part of ISO/IEC
Alex Stapleton79043462014-03-09 16:46:26 +0000130 10118-3:2004. It produces a 512-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500131
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500132MD5
Matthew Iversen505491b2013-10-19 15:56:17 +1100133~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500134
135.. warning::
136
137 MD5 is a deprecated hash algorithm that has practical known collision
Alex Gaynorab5f0112013-11-08 10:34:00 -0800138 attacks. You are strongly discouraged from using it. Existing applications
139 should strongly consider moving away.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500140
David Reid1f3d7182013-10-22 16:55:18 -0700141.. class:: MD5()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500142
Alex Stapleton79043462014-03-09 16:46:26 +0000143 MD5 is a deprecated cryptographic hash function. It produces a 128-bit
144 message digest and has practical known collision attacks.
Alex Gaynor94801292013-11-13 10:33:01 -0800145
146
147.. _`Lifetimes of cryptographic hash functions`: http://valerieaurora.org/hash.html