Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 1 | .. hazmat:: |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 2 | |
| 3 | Hash-based Message Authentication Codes |
| 4 | ======================================= |
| 5 | |
Alex Gaynor | 4658ce1 | 2013-10-29 15:26:50 -0700 | [diff] [blame] | 6 | .. currentmodule:: cryptography.hazmat.primitives.hmac |
| 7 | |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 8 | .. testsetup:: |
| 9 | |
| 10 | import binascii |
| 11 | key = binascii.unhexlify(b"0" * 32) |
| 12 | |
| 13 | Hash-based message authentication codes (or HMACs) are a tool for calculating |
| 14 | message authentication codes using a cryptographic hash function coupled with a |
| 15 | secret key. You can use an HMAC to verify integrity as well as authenticate a |
| 16 | message. |
| 17 | |
David Reid | 6753e39 | 2013-11-01 15:32:03 -0700 | [diff] [blame] | 18 | .. class:: HMAC(key, algorithm) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 19 | |
David Reid | 6753e39 | 2013-11-01 15:32:03 -0700 | [diff] [blame] | 20 | HMAC objects take a ``key`` and a provider of |
| 21 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`. |
Paul Kehrer | 50a8815 | 2013-10-29 10:46:05 -0500 | [diff] [blame] | 22 | The ``key`` should be randomly generated bytes and is recommended to be |
| 23 | equal in length to the ``digest_size`` of the hash function chosen. |
| 24 | You must keep the ``key`` secret. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 25 | |
Alex Gaynor | 58ecc8d | 2013-11-03 21:21:00 -0800 | [diff] [blame] | 26 | This is an implementation of :rfc:`2104`. |
| 27 | |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 28 | .. doctest:: |
| 29 | |
Paul Kehrer | bf8962a | 2013-10-28 17:44:42 -0500 | [diff] [blame] | 30 | >>> from cryptography.hazmat.primitives import hashes, hmac |
David Reid | 6753e39 | 2013-11-01 15:32:03 -0700 | [diff] [blame] | 31 | >>> h = hmac.HMAC(key, hashes.SHA256()) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 32 | >>> h.update(b"message to hash") |
David Reid | 6753e39 | 2013-11-01 15:32:03 -0700 | [diff] [blame] | 33 | >>> h.finalize() |
| 34 | '#F\xdaI\x8b"e\xc4\xf1\xbb\x9a\x8fc\xff\xf5\xdex.\xbc\xcd/+\x8a\x86\x1d\x84\'\xc3\xa6\x1d\xd8J' |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 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 | 50a8815 | 2013-10-29 10:46:05 -0500 | [diff] [blame] | 38 | :param bytes msg: The bytes to hash and authenticate. |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 39 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 40 | |
| 41 | .. method:: copy() |
| 42 | |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 43 | Copy this :class:`HMAC` instance, usually so that we may call |
| 44 | :meth:`finalize` and get an intermediate digest value while we continue |
| 45 | to call :meth:`update` on the original. |
| 46 | |
| 47 | :return: A new instance of :class:`HMAC` which can be updated |
| 48 | and finalized independently of the original instance. |
| 49 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 50 | |
David Reid | 6753e39 | 2013-11-01 15:32:03 -0700 | [diff] [blame] | 51 | .. method:: finalize() |
| 52 | |
| 53 | Finalize the current context and return the message digest as bytes. |
| 54 | |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 55 | Once ``finalize`` is called this object can no longer be used and |
| 56 | :meth:`update`, :meth:`copy`, and :meth:`finalize` will raise |
| 57 | :class:`~cryptography.exceptions.AlreadyFinalized`. |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 58 | |
| 59 | :return bytes: The message digest as bytes. |
David Reid | 2cce618 | 2013-11-13 13:49:41 -0800 | [diff] [blame] | 60 | :raises cryptography.exceptions.AlreadyFinalized: |