Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 1 | .. hazmat:: |
Donald Stufft | d8f0118 | 2013-10-27 16:59:56 -0400 | [diff] [blame] | 2 | |
Paul Kehrer | 0db218e | 2017-05-29 17:09:05 -0500 | [diff] [blame] | 3 | Message digests (Hashing) |
| 4 | ========================= |
Donald Stufft | e51fb93 | 2013-10-27 17:26:17 -0400 | [diff] [blame] | 5 | |
Paul Kehrer | 45efdbc | 2015-02-12 10:58:22 -0600 | [diff] [blame] | 6 | .. module:: cryptography.hazmat.primitives.hashes |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 7 | |
David Reid | ef0fcf2 | 2013-11-06 11:12:45 -0800 | [diff] [blame] | 8 | .. class:: Hash(algorithm, backend) |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 9 | |
David Reid | 5560298 | 2013-11-01 13:34:05 -0700 | [diff] [blame] | 10 | 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 Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 13 | |
David Reid | 5560298 | 2013-11-01 13:34:05 -0700 | [diff] [blame] | 14 | This is an implementation of |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 15 | :class:`~cryptography.hazmat.primitives.hashes.HashContext` meant to |
David Reid | 5560298 | 2013-11-01 13:34:05 -0700 | [diff] [blame] | 16 | be used with |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 17 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
David Reid | 5560298 | 2013-11-01 13:34:05 -0700 | [diff] [blame] | 18 | implementations to provide an incremental interface to calculating |
| 19 | various message digests. |
Alex Gaynor | 23d01a2 | 2013-10-28 10:14:46 -0700 | [diff] [blame] | 20 | |
| 21 | .. doctest:: |
David Reid | 846460a | 2013-11-06 11:24:50 -0800 | [diff] [blame] | 22 | |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 23 | >>> from cryptography.hazmat.backends import default_backend |
Alex Gaynor | 23d01a2 | 2013-10-28 10:14:46 -0700 | [diff] [blame] | 24 | >>> from cryptography.hazmat.primitives import hashes |
David Reid | 63fa19a | 2013-11-20 10:49:13 -0800 | [diff] [blame] | 25 | >>> digest = hashes.Hash(hashes.SHA256(), backend=default_backend()) |
Alex Gaynor | 23d01a2 | 2013-10-28 10:14:46 -0700 | [diff] [blame] | 26 | >>> digest.update(b"abc") |
| 27 | >>> digest.update(b"123") |
David Reid | 30b1613 | 2013-10-31 13:37:24 -0700 | [diff] [blame] | 28 | >>> digest.finalize() |
Paul Kehrer | 056c9dd | 2018-05-12 15:17:06 -0400 | [diff] [blame] | 29 | 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 Gaynor | f3b06cd | 2013-10-21 21:49:50 -0700 | [diff] [blame] | 30 | |
Alex Stapleton | 1b1327c | 2013-12-21 15:16:57 +0000 | [diff] [blame] | 31 | If the backend doesn't support the requested ``algorithm`` an |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 32 | :class:`~cryptography.exceptions.UnsupportedAlgorithm` exception will be |
| 33 | raised. |
Alex Stapleton | 1b1327c | 2013-12-21 15:16:57 +0000 | [diff] [blame] | 34 | |
Alex Gaynor | 9480129 | 2013-11-13 10:33:01 -0800 | [diff] [blame] | 35 | 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 Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 41 | :param algorithm: A |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 42 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 43 | instance such as those described in |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 44 | :ref:`below <cryptographic-hash-algorithms>`. |
| 45 | :param backend: A |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 46 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 47 | instance. |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 48 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 49 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
Ayrx | b482ca1 | 2014-03-16 13:06:25 +0800 | [diff] [blame] | 50 | provided ``backend`` does not implement |
| 51 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 52 | |
Paul Kehrer | 6b9ddeb | 2013-10-19 12:28:15 -0500 | [diff] [blame] | 53 | .. method:: update(data) |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 54 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 55 | :param bytes data: The bytes to be hashed. |
| 56 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`. |
Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 57 | :raises TypeError: This exception is raised if ``data`` is not ``bytes``. |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 58 | |
| 59 | .. method:: copy() |
| 60 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 61 | 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 Reid | 6392a9c | 2013-11-13 10:01:15 -0800 | [diff] [blame] | 64 | |
Alex Stapleton | 63b3de2 | 2014-02-08 09:43:16 +0000 | [diff] [blame] | 65 | :return: A new instance of :class:`Hash` that can be updated |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 66 | and finalized independently of the original instance. |
| 67 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`. |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 68 | |
David Reid | 30b1613 | 2013-10-31 13:37:24 -0700 | [diff] [blame] | 69 | .. method:: finalize() |
Alex Gaynor | 1496845 | 2013-11-01 14:05:14 -0700 | [diff] [blame] | 70 | |
David Reid | 5560298 | 2013-11-01 13:34:05 -0700 | [diff] [blame] | 71 | Finalize the current context and return the message digest as bytes. |
| 72 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 73 | 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 Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 76 | |
| 77 | :return bytes: The message digest as bytes. |
| 78 | |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 79 | |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 80 | .. _cryptographic-hash-algorithms: |
| 81 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 82 | SHA-2 family |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 83 | ~~~~~~~~~~~~ |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 84 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 85 | .. class:: SHA224() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 86 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 87 | 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 Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 89 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 90 | .. class:: SHA256() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 91 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 92 | 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 Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 94 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 95 | .. class:: SHA384() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 96 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 97 | 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 Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 99 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 100 | .. class:: SHA512() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 101 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 102 | 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 Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 104 | |
Paul Kehrer | d67fa82 | 2018-11-12 21:41:26 -0500 | [diff] [blame] | 105 | .. 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 Kehrer | 306ce51 | 2016-08-29 09:36:09 +0800 | [diff] [blame] | 119 | BLAKE2 |
| 120 | ~~~~~~ |
| 121 | |
Alex Gaynor | a2bf0ea | 2016-08-28 23:15:37 -0400 | [diff] [blame] | 122 | `BLAKE2`_ is a cryptographic hash function specified in :rfc:`7693`. BLAKE2's |
| 123 | design makes it immune to `length-extension attacks`_, an advantage over the |
| 124 | SHA-family of hashes. |
Paul Kehrer | 306ce51 | 2016-08-29 09:36:09 +0800 | [diff] [blame] | 125 | |
| 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 | |
Paul Kehrer | 3065e16 | 2018-11-22 23:42:42 +0800 | [diff] [blame^] | 151 | SHA-3 family |
| 152 | ~~~~~~~~~~~~ |
| 153 | |
| 154 | SHA-3 is the most recent NIST secure hash algorithm standard. Despite the |
| 155 | larger number SHA-3 is not considered to be better than SHA-2. Instead, it uses |
| 156 | a significantly different internal structure so that **if** an attack appears |
| 157 | against SHA-2 it is unlikely to apply to SHA-3. SHA-3 is significantly slower |
| 158 | than SHA-2 so at this time most users should choose SHA-2. |
| 159 | |
| 160 | .. class:: SHA3_224() |
| 161 | |
| 162 | .. versionadded:: 2.5 |
| 163 | |
| 164 | SHA3/224 is a cryptographic hash function from the SHA-3 family and is |
| 165 | standardized by NIST. It produces a 224-bit message digest. |
| 166 | |
| 167 | .. class:: SHA3_256() |
| 168 | |
| 169 | .. versionadded:: 2.5 |
| 170 | |
| 171 | SHA3/256 is a cryptographic hash function from the SHA-3 family and is |
| 172 | standardized by NIST. It produces a 256-bit message digest. |
| 173 | |
| 174 | .. class:: SHA3_384() |
| 175 | |
| 176 | .. versionadded:: 2.5 |
| 177 | |
| 178 | SHA3/384 is a cryptographic hash function from the SHA-3 family and is |
| 179 | standardized by NIST. It produces a 384-bit message digest. |
| 180 | |
| 181 | .. class:: SHA3_512() |
| 182 | |
| 183 | .. versionadded:: 2.5 |
| 184 | |
| 185 | SHA3/512 is a cryptographic hash function from the SHA-3 family and is |
| 186 | standardized by NIST. It produces a 512-bit message digest. |
| 187 | |
Alex Gaynor | 500047b | 2017-02-23 13:57:55 -0500 | [diff] [blame] | 188 | SHA-1 |
| 189 | ~~~~~ |
| 190 | |
| 191 | .. warning:: |
| 192 | |
| 193 | SHA-1 is a deprecated hash algorithm that has practical known collision |
| 194 | attacks. You are strongly discouraged from using it. Existing applications |
| 195 | should strongly consider moving away. |
| 196 | |
| 197 | .. class:: SHA1() |
| 198 | |
| 199 | SHA-1 is a cryptographic hash function standardized by NIST. It produces an |
| 200 | 160-bit message digest. Cryptanalysis of SHA-1 has demonstrated that it is |
| 201 | vulnerable to practical collision attacks, and collisions have been |
| 202 | demonstrated. |
| 203 | |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 204 | MD5 |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 205 | ~~~ |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 206 | |
| 207 | .. warning:: |
| 208 | |
| 209 | MD5 is a deprecated hash algorithm that has practical known collision |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 210 | attacks. You are strongly discouraged from using it. Existing applications |
| 211 | should strongly consider moving away. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 212 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 213 | .. class:: MD5() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 214 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 215 | MD5 is a deprecated cryptographic hash function. It produces a 128-bit |
| 216 | message digest and has practical known collision attacks. |
Alex Gaynor | 9480129 | 2013-11-13 10:33:01 -0800 | [diff] [blame] | 217 | |
| 218 | |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 219 | Interfaces |
| 220 | ~~~~~~~~~~ |
| 221 | |
| 222 | .. class:: HashAlgorithm |
| 223 | |
| 224 | .. attribute:: name |
| 225 | |
| 226 | :type: str |
| 227 | |
| 228 | The standard name for the hash algorithm, for example: ``"sha256"`` or |
Alex Gaynor | 3e38506 | 2017-12-10 19:54:02 -0500 | [diff] [blame] | 229 | ``"blake2b"``. |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 230 | |
| 231 | .. attribute:: digest_size |
| 232 | |
| 233 | :type: int |
| 234 | |
| 235 | The size of the resulting digest in bytes. |
| 236 | |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 237 | |
| 238 | .. class:: HashContext |
| 239 | |
| 240 | .. attribute:: algorithm |
| 241 | |
| 242 | A :class:`HashAlgorithm` that will be used by this context. |
| 243 | |
| 244 | .. method:: update(data) |
| 245 | |
| 246 | :param bytes data: The data you want to hash. |
| 247 | |
| 248 | .. method:: finalize() |
| 249 | |
| 250 | :return: The final digest as bytes. |
| 251 | |
| 252 | .. method:: copy() |
| 253 | |
| 254 | :return: A :class:`HashContext` that is a copy of the current context. |
| 255 | |
| 256 | |
Alex Gaynor | 9a59ad8 | 2017-02-23 05:38:41 -0500 | [diff] [blame] | 257 | .. _`Lifetimes of cryptographic hash functions`: https://valerieaurora.org/hash.html |
Paul Kehrer | 306ce51 | 2016-08-29 09:36:09 +0800 | [diff] [blame] | 258 | .. _`BLAKE2`: https://blake2.net |
Alex Gaynor | a2bf0ea | 2016-08-28 23:15:37 -0400 | [diff] [blame] | 259 | .. _`length-extension attacks`: https://en.wikipedia.org/wiki/Length_extension_attack |