Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 1 | .. danger:: |
| 2 | |
| 3 | This is a "Hazardous Materials" module. You should **ONLY** use it if |
| 4 | you're 100% absolutely sure that you know what you're doing because this |
| 5 | module is full of land mines, dragons, and dinosaurs with laser guns. |
| 6 | |
| 7 | |
| 8 | Hash-based Message Authentication Codes |
| 9 | ======================================= |
| 10 | |
| 11 | .. testsetup:: |
| 12 | |
| 13 | import binascii |
| 14 | key = binascii.unhexlify(b"0" * 32) |
| 15 | |
| 16 | Hash-based message authentication codes (or HMACs) are a tool for calculating |
| 17 | message authentication codes using a cryptographic hash function coupled with a |
| 18 | secret key. You can use an HMAC to verify integrity as well as authenticate a |
| 19 | message. |
| 20 | |
Paul Kehrer | bf8962a | 2013-10-28 17:44:42 -0500 | [diff] [blame] | 21 | .. class:: cryptography.hazmat.primitives.hmac.HMAC(key, msg=None, digestmod=None) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 22 | |
Paul Kehrer | ca8ed29 | 2013-10-28 19:37:39 -0500 | [diff] [blame] | 23 | HMAC objects take a ``key``, a hash class derived from |
Paul Kehrer | 50a8815 | 2013-10-29 10:46:05 -0500 | [diff] [blame^] | 24 | :class:`~cryptography.primitives.hashes.BaseHash`, and optional message. |
| 25 | The ``key`` should be randomly generated bytes and is recommended to be |
| 26 | equal in length to the ``digest_size`` of the hash function chosen. |
| 27 | You must keep the ``key`` secret. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 28 | |
| 29 | .. doctest:: |
| 30 | |
Paul Kehrer | bf8962a | 2013-10-28 17:44:42 -0500 | [diff] [blame] | 31 | >>> from cryptography.hazmat.primitives import hashes, hmac |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 32 | >>> h = hmac.HMAC(key, digestmod=hashes.SHA256) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 33 | >>> h.update(b"message to hash") |
| 34 | >>> h.hexdigest() |
| 35 | '...' |
| 36 | |
Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 37 | .. method:: update(msg) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 38 | |
Paul Kehrer | 50a8815 | 2013-10-29 10:46:05 -0500 | [diff] [blame^] | 39 | :param bytes msg: The bytes to hash and authenticate. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 40 | |
| 41 | .. method:: copy() |
| 42 | |
| 43 | :return: a new instance of this object with a copied internal state. |
| 44 | |
| 45 | .. method:: digest() |
| 46 | |
| 47 | :return bytes: The message digest as bytes. |
| 48 | |
| 49 | .. method:: hexdigest() |
| 50 | |
| 51 | :return str: The message digest as hex. |
| 52 | |