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 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 3 | Message digests |
Donald Stufft | e51fb93 | 2013-10-27 17:26:17 -0400 | [diff] [blame] | 4 | =============== |
| 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() |
| 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 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 | |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 82 | SHA-1 |
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 | |
| 85 | .. attention:: |
| 86 | |
| 87 | NIST has deprecated SHA-1 in favor of the SHA-2 variants. New applications |
| 88 | are strongly suggested to use SHA-2 over SHA-1. |
| 89 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 90 | .. class:: SHA1() |
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-1 is a cryptographic hash function standardized by NIST. It produces an |
Alex Gaynor | 0255818 | 2016-05-29 14:30:50 -0400 | [diff] [blame] | 93 | 160-bit message digest. Cryptanalysis of SHA-1 has demonstrated that it is |
| 94 | vulnerable to practical collision attacks, though no actual collisions are |
| 95 | publicly known. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 96 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 97 | SHA-2 family |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 98 | ~~~~~~~~~~~~ |
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:: SHA224() |
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-224 is a cryptographic hash function from the SHA-2 family and is |
| 103 | standardized by NIST. It produces a 224-bit message digest. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 104 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 105 | .. class:: SHA256() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 106 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 107 | SHA-256 is a cryptographic hash function from the SHA-2 family and is |
| 108 | standardized by NIST. It produces a 256-bit message digest. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 109 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 110 | .. class:: SHA384() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 111 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 112 | SHA-384 is a cryptographic hash function from the SHA-2 family and is |
| 113 | standardized by NIST. It produces a 384-bit message digest. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 114 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 115 | .. class:: SHA512() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 116 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 117 | SHA-512 is a cryptographic hash function from the SHA-2 family and is |
| 118 | standardized by NIST. It produces a 512-bit message digest. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 119 | |
Paul Kehrer | 306ce51 | 2016-08-29 09:36:09 +0800 | [diff] [blame] | 120 | BLAKE2 |
| 121 | ~~~~~~ |
| 122 | |
Alex Gaynor | a2bf0ea | 2016-08-28 23:15:37 -0400 | [diff] [blame^] | 123 | `BLAKE2`_ is a cryptographic hash function specified in :rfc:`7693`. BLAKE2's |
| 124 | design makes it immune to `length-extension attacks`_, an advantage over the |
| 125 | SHA-family of hashes. |
Paul Kehrer | 306ce51 | 2016-08-29 09:36:09 +0800 | [diff] [blame] | 126 | |
| 127 | .. note:: |
| 128 | |
| 129 | While the RFC specifies keying, personalization, and salting features, |
| 130 | these are not supported at this time due to limitations in OpenSSL 1.1.0. |
| 131 | |
| 132 | .. class:: BLAKE2b(digest_size) |
| 133 | |
| 134 | BLAKE2b is optimized for 64-bit platforms and produces an 1 to 64-byte |
| 135 | message digest. |
| 136 | |
| 137 | :param int digest_size: The desired size of the hash output in bytes. Only |
| 138 | ``64`` is supported at this time. |
| 139 | |
| 140 | :raises ValueError: If the ``digest_size`` is invalid. |
| 141 | |
| 142 | .. class:: BLAKE2s(digest_size) |
| 143 | |
| 144 | BLAKE2s is optimized for 8 to 32-bit platforms and produces a |
| 145 | 1 to 32-byte message digest. |
| 146 | |
| 147 | :param int digest_size: The desired size of the hash output in bytes. Only |
| 148 | ``32`` is supported at this time. |
| 149 | |
| 150 | :raises ValueError: If the ``digest_size`` is invalid. |
| 151 | |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 152 | RIPEMD160 |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 153 | ~~~~~~~~~ |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 154 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 155 | .. class:: RIPEMD160() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 156 | |
| 157 | RIPEMD160 is a cryptographic hash function that is part of ISO/IEC |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 158 | 10118-3:2004. It produces a 160-bit message digest. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 159 | |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 160 | Whirlpool |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 161 | ~~~~~~~~~ |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 162 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 163 | .. class:: Whirlpool() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 164 | |
| 165 | Whirlpool is a cryptographic hash function that is part of ISO/IEC |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 166 | 10118-3:2004. It produces a 512-bit message digest. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 167 | |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 168 | MD5 |
Matthew Iversen | 505491b | 2013-10-19 15:56:17 +1100 | [diff] [blame] | 169 | ~~~ |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 170 | |
| 171 | .. warning:: |
| 172 | |
| 173 | MD5 is a deprecated hash algorithm that has practical known collision |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 174 | attacks. You are strongly discouraged from using it. Existing applications |
| 175 | should strongly consider moving away. |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 176 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 177 | .. class:: MD5() |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 178 | |
Alex Stapleton | 7904346 | 2014-03-09 16:46:26 +0000 | [diff] [blame] | 179 | MD5 is a deprecated cryptographic hash function. It produces a 128-bit |
| 180 | message digest and has practical known collision attacks. |
Alex Gaynor | 9480129 | 2013-11-13 10:33:01 -0800 | [diff] [blame] | 181 | |
| 182 | |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 183 | Interfaces |
| 184 | ~~~~~~~~~~ |
| 185 | |
| 186 | .. class:: HashAlgorithm |
| 187 | |
| 188 | .. attribute:: name |
| 189 | |
| 190 | :type: str |
| 191 | |
| 192 | The standard name for the hash algorithm, for example: ``"sha256"`` or |
| 193 | ``"whirlpool"``. |
| 194 | |
| 195 | .. attribute:: digest_size |
| 196 | |
| 197 | :type: int |
| 198 | |
| 199 | The size of the resulting digest in bytes. |
| 200 | |
| 201 | .. attribute:: block_size |
| 202 | |
| 203 | :type: int |
| 204 | |
| 205 | The internal block size of the hash algorithm in bytes. |
| 206 | |
| 207 | |
| 208 | .. class:: HashContext |
| 209 | |
| 210 | .. attribute:: algorithm |
| 211 | |
| 212 | A :class:`HashAlgorithm` that will be used by this context. |
| 213 | |
| 214 | .. method:: update(data) |
| 215 | |
| 216 | :param bytes data: The data you want to hash. |
| 217 | |
| 218 | .. method:: finalize() |
| 219 | |
| 220 | :return: The final digest as bytes. |
| 221 | |
| 222 | .. method:: copy() |
| 223 | |
| 224 | :return: A :class:`HashContext` that is a copy of the current context. |
| 225 | |
| 226 | |
Alex Gaynor | 9480129 | 2013-11-13 10:33:01 -0800 | [diff] [blame] | 227 | .. _`Lifetimes of cryptographic hash functions`: http://valerieaurora.org/hash.html |
Paul Kehrer | 306ce51 | 2016-08-29 09:36:09 +0800 | [diff] [blame] | 228 | .. _`BLAKE2`: https://blake2.net |
Alex Gaynor | a2bf0ea | 2016-08-28 23:15:37 -0400 | [diff] [blame^] | 229 | .. _`length-extension attacks`: https://en.wikipedia.org/wiki/Length_extension_attack |