blob: 1a96fc2dd22f1d56497b0500def778315b19f5f6 [file] [log] [blame]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07001.. hazmat::
Donald Stufftd8f01182013-10-27 16:59:56 -04002
Paul Kehrer0db218e2017-05-29 17:09:05 -05003Message digests (Hashing)
4=========================
Donald Stuffte51fb932013-10-27 17:26:17 -04005
Paul Kehrer45efdbc2015-02-12 10:58:22 -06006.. module:: 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
Paul Kehrer601278a2015-02-12 12:51:00 -060015 :class:`~cryptography.hazmat.primitives.hashes.HashContext` meant to
David Reid55602982013-11-01 13:34:05 -070016 be used with
Paul Kehrer601278a2015-02-12 12:51:00 -060017 :class:`~cryptography.hazmat.primitives.hashes.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()
Paul Kehrer056c9dd2018-05-12 15:17:06 -040029 b'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 Gaynor7a489db2014-03-22 15:09:34 -070032 :class:`~cryptography.exceptions.UnsupportedAlgorithm` exception will be
33 raised.
Alex Stapleton1b1327c2013-12-21 15:16:57 +000034
Alex Gaynor94801292013-11-13 10:33:01 -080035 Keep in mind that attacks against cryptographic hashes only get stronger
36 with time, and that often algorithms that were once thought to be strong,
37 become broken. Because of this it's important to include a plan for
38 upgrading the hash algorithm you use over time. For more information, see
39 `Lifetimes of cryptographic hash functions`_.
40
David Reid663295d2013-11-20 13:55:08 -080041 :param algorithm: A
Paul Kehrer601278a2015-02-12 12:51:00 -060042 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
Gabriel Orisaka617fe4b2016-07-31 10:49:59 -030043 instance such as those described in
David Reid663295d2013-11-20 13:55:08 -080044 :ref:`below <cryptographic-hash-algorithms>`.
45 :param backend: A
Alex Gaynorf8796b12013-12-13 20:28:55 -080046 :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
Gabriel Orisaka617fe4b2016-07-31 10:49:59 -030047 instance.
David Reid663295d2013-11-20 13:55:08 -080048
Alex Gaynor7a489db2014-03-22 15:09:34 -070049 :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the
Ayrxb482ca12014-03-16 13:06:25 +080050 provided ``backend`` does not implement
51 :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
52
Paul Kehrer6b9ddeb2013-10-19 12:28:15 -050053 .. method:: update(data)
Matthew Iversen505491b2013-10-19 15:56:17 +110054
Alex Stapleton79043462014-03-09 16:46:26 +000055 :param bytes data: The bytes to be hashed.
56 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`.
Ayrx00eff9c2014-05-17 19:47:09 +080057 :raises TypeError: This exception is raised if ``data`` is not ``bytes``.
Matthew Iversen505491b2013-10-19 15:56:17 +110058
59 .. method:: copy()
60
Alex Stapleton79043462014-03-09 16:46:26 +000061 Copy this :class:`Hash` instance, usually so that you may call
62 :meth:`finalize` to get an intermediate digest value while we continue
63 to call :meth:`update` on the original instance.
David Reid6392a9c2013-11-13 10:01:15 -080064
Alex Stapleton63b3de22014-02-08 09:43:16 +000065 :return: A new instance of :class:`Hash` that can be updated
Alex Stapleton79043462014-03-09 16:46:26 +000066 and finalized independently of the original instance.
67 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`.
Matthew Iversen505491b2013-10-19 15:56:17 +110068
David Reid30b16132013-10-31 13:37:24 -070069 .. method:: finalize()
Alex Gaynor14968452013-11-01 14:05:14 -070070
David Reid55602982013-11-01 13:34:05 -070071 Finalize the current context and return the message digest as bytes.
72
Alex Stapleton79043462014-03-09 16:46:26 +000073 After ``finalize`` has been called this object can no longer be used
74 and :meth:`update`, :meth:`copy`, and :meth:`finalize` will raise an
75 :class:`~cryptography.exceptions.AlreadyFinalized` exception.
Matthew Iversen505491b2013-10-19 15:56:17 +110076
77 :return bytes: The message digest as bytes.
78
Matthew Iversen505491b2013-10-19 15:56:17 +110079
David Reid663295d2013-11-20 13:55:08 -080080.. _cryptographic-hash-algorithms:
81
Alex Stapletonc5fffd32014-03-18 15:29:00 +000082SHA-2 family
Matthew Iversen505491b2013-10-19 15:56:17 +110083~~~~~~~~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050084
David Reid1f3d7182013-10-22 16:55:18 -070085.. class:: SHA224()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050086
Alex Stapleton79043462014-03-09 16:46:26 +000087 SHA-224 is a cryptographic hash function from the SHA-2 family and is
88 standardized by NIST. It produces a 224-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050089
David Reid1f3d7182013-10-22 16:55:18 -070090.. class:: SHA256()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050091
Alex Stapleton79043462014-03-09 16:46:26 +000092 SHA-256 is a cryptographic hash function from the SHA-2 family and is
93 standardized by NIST. It produces a 256-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050094
David Reid1f3d7182013-10-22 16:55:18 -070095.. class:: SHA384()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050096
Alex Stapleton79043462014-03-09 16:46:26 +000097 SHA-384 is a cryptographic hash function from the SHA-2 family and is
98 standardized by NIST. It produces a 384-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050099
David Reid1f3d7182013-10-22 16:55:18 -0700100.. class:: SHA512()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500101
Alex Stapleton79043462014-03-09 16:46:26 +0000102 SHA-512 is a cryptographic hash function from the SHA-2 family and is
103 standardized by NIST. It produces a 512-bit message digest.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500104
Paul Kehrerd67fa822018-11-12 21:41:26 -0500105.. class:: SHA512_224()
106
107 .. versionadded:: 2.5
108
109 SHA-512/224 is a cryptographic hash function from the SHA-2 family and is
110 standardized by NIST. It produces a 224-bit message digest.
111
112.. class:: SHA512_256()
113
114 .. versionadded:: 2.5
115
116 SHA-512/256 is a cryptographic hash function from the SHA-2 family and is
117 standardized by NIST. It produces a 256-bit message digest.
118
Paul Kehrer306ce512016-08-29 09:36:09 +0800119BLAKE2
120~~~~~~
121
Alex Gaynora2bf0ea2016-08-28 23:15:37 -0400122`BLAKE2`_ is a cryptographic hash function specified in :rfc:`7693`. BLAKE2's
123design makes it immune to `length-extension attacks`_, an advantage over the
124SHA-family of hashes.
Paul Kehrer306ce512016-08-29 09:36:09 +0800125
126.. note::
127
128 While the RFC specifies keying, personalization, and salting features,
129 these are not supported at this time due to limitations in OpenSSL 1.1.0.
130
131.. class:: BLAKE2b(digest_size)
132
133 BLAKE2b is optimized for 64-bit platforms and produces an 1 to 64-byte
134 message digest.
135
136 :param int digest_size: The desired size of the hash output in bytes. Only
137 ``64`` is supported at this time.
138
139 :raises ValueError: If the ``digest_size`` is invalid.
140
141.. class:: BLAKE2s(digest_size)
142
143 BLAKE2s is optimized for 8 to 32-bit platforms and produces a
144 1 to 32-byte message digest.
145
146 :param int digest_size: The desired size of the hash output in bytes. Only
147 ``32`` is supported at this time.
148
149 :raises ValueError: If the ``digest_size`` is invalid.
150
Alex Gaynor500047b2017-02-23 13:57:55 -0500151SHA-1
152~~~~~
153
154.. warning::
155
156 SHA-1 is a deprecated hash algorithm that has practical known collision
157 attacks. You are strongly discouraged from using it. Existing applications
158 should strongly consider moving away.
159
160.. class:: SHA1()
161
162 SHA-1 is a cryptographic hash function standardized by NIST. It produces an
163 160-bit message digest. Cryptanalysis of SHA-1 has demonstrated that it is
164 vulnerable to practical collision attacks, and collisions have been
165 demonstrated.
166
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500167MD5
Matthew Iversen505491b2013-10-19 15:56:17 +1100168~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500169
170.. warning::
171
172 MD5 is a deprecated hash algorithm that has practical known collision
Alex Gaynorab5f0112013-11-08 10:34:00 -0800173 attacks. You are strongly discouraged from using it. Existing applications
174 should strongly consider moving away.
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500175
David Reid1f3d7182013-10-22 16:55:18 -0700176.. class:: MD5()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500177
Alex Stapleton79043462014-03-09 16:46:26 +0000178 MD5 is a deprecated cryptographic hash function. It produces a 128-bit
179 message digest and has practical known collision attacks.
Alex Gaynor94801292013-11-13 10:33:01 -0800180
181
Paul Kehrer601278a2015-02-12 12:51:00 -0600182Interfaces
183~~~~~~~~~~
184
185.. class:: HashAlgorithm
186
187 .. attribute:: name
188
189 :type: str
190
191 The standard name for the hash algorithm, for example: ``"sha256"`` or
Alex Gaynor3e385062017-12-10 19:54:02 -0500192 ``"blake2b"``.
Paul Kehrer601278a2015-02-12 12:51:00 -0600193
194 .. attribute:: digest_size
195
196 :type: int
197
198 The size of the resulting digest in bytes.
199
Paul Kehrer601278a2015-02-12 12:51:00 -0600200
201.. class:: HashContext
202
203 .. attribute:: algorithm
204
205 A :class:`HashAlgorithm` that will be used by this context.
206
207 .. method:: update(data)
208
209 :param bytes data: The data you want to hash.
210
211 .. method:: finalize()
212
213 :return: The final digest as bytes.
214
215 .. method:: copy()
216
217 :return: A :class:`HashContext` that is a copy of the current context.
218
219
Alex Gaynor9a59ad82017-02-23 05:38:41 -0500220.. _`Lifetimes of cryptographic hash functions`: https://valerieaurora.org/hash.html
Paul Kehrer306ce512016-08-29 09:36:09 +0800221.. _`BLAKE2`: https://blake2.net
Alex Gaynora2bf0ea2016-08-28 23:15:37 -0400222.. _`length-extension attacks`: https://en.wikipedia.org/wiki/Length_extension_attack