| 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 | 
|  | 24 | :class:`~cryptography.primitives.hashes.BaseHash`, and optional msg.  The | 
|  | 25 | ``key`` should be randomly generated bytes and the length of the | 
|  | 26 | ``block_size`` of the hash. You must keep the ``key`` secret. | 
| Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 27 |  | 
|  | 28 | .. doctest:: | 
|  | 29 |  | 
| Paul Kehrer | bf8962a | 2013-10-28 17:44:42 -0500 | [diff] [blame] | 30 | >>> from cryptography.hazmat.primitives import hashes, hmac | 
| Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 31 | >>> h = hmac.HMAC(key, digestmod=hashes.SHA256) | 
| Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 32 | >>> h.update(b"message to hash") | 
|  | 33 | >>> h.hexdigest() | 
|  | 34 | '...' | 
|  | 35 |  | 
| Paul Kehrer | 2824ab7 | 2013-10-28 11:06:55 -0500 | [diff] [blame] | 36 | .. method:: update(msg) | 
| Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 37 |  | 
| Paul Kehrer | 30eabdd | 2013-10-28 12:52:47 -0500 | [diff] [blame] | 38 | :param bytes msg: The bytes you wish to hash. | 
| Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 39 |  | 
|  | 40 | .. method:: copy() | 
|  | 41 |  | 
|  | 42 | :return: a new instance of this object with a copied internal state. | 
|  | 43 |  | 
|  | 44 | .. method:: digest() | 
|  | 45 |  | 
|  | 46 | :return bytes: The message digest as bytes. | 
|  | 47 |  | 
|  | 48 | .. method:: hexdigest() | 
|  | 49 |  | 
|  | 50 | :return str: The message digest as hex. | 
|  | 51 |  |