blob: a939998d5d938ef0e2a98260b459192352b09eb7 [file] [log] [blame]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07001.. hazmat::
Donald Stufftd8f01182013-10-27 16:59:56 -04002
Donald Stuffte51fb932013-10-27 17:26:17 -04003Message Digests
4===============
5
Donald Stufftf04317a2013-10-27 16:44:30 -04006.. currentmodule:: cryptography.hazmat.primitives.hashes
David Reid1f3d7182013-10-22 16:55:18 -07007
David Reid30b16132013-10-31 13:37:24 -07008.. class:: Hash(algorithm)
Matthew Iversen505491b2013-10-19 15:56:17 +11009
David Reid55602982013-11-01 13:34:05 -070010 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 Iversen505491b2013-10-19 15:56:17 +110013
David Reid55602982013-11-01 13:34:05 -070014 This is an implementation of
15 :class:`cryptography.hazmat.primitives.interfaces.HashContext` meant to
16 be used with
17 :class:`cryptography.hazmat.primitives.interfaces.HashAlgorithm`
18 implementations to provide an incremental interface to calculating
19 various message digests.
Alex Gaynor23d01a22013-10-28 10:14:46 -070020
21 .. doctest::
22
23 >>> from cryptography.hazmat.primitives import hashes
David Reid30b16132013-10-31 13:37:24 -070024 >>> digest = hashes.Hash(hashes.SHA256())
Alex Gaynor23d01a22013-10-28 10:14:46 -070025 >>> digest.update(b"abc")
26 >>> digest.update(b"123")
David Reid30b16132013-10-31 13:37:24 -070027 >>> digest.finalize()
28 'l\xa1=R\xcap\xc8\x83\xe0\xf0\xbb\x10\x1eBZ\x89\xe8bM\xe5\x1d\xb2\xd29%\x93\xafj\x84\x11\x80\x90'
Alex Gaynorf3b06cd2013-10-21 21:49:50 -070029
Paul Kehrer6b9ddeb2013-10-19 12:28:15 -050030 .. method:: update(data)
Matthew Iversen505491b2013-10-19 15:56:17 +110031
Alex Gaynorddc62f02013-10-20 06:14:24 -070032 :param bytes data: The bytes you wish to hash.
Matthew Iversen505491b2013-10-19 15:56:17 +110033
34 .. method:: copy()
35
Paul Kehrer6b9ddeb2013-10-19 12:28:15 -050036 :return: a new instance of this object with a copied internal state.
Matthew Iversen505491b2013-10-19 15:56:17 +110037
David Reid30b16132013-10-31 13:37:24 -070038 .. method:: finalize()
David Reid55602982013-11-01 13:34:05 -070039 Finalize the current context and return the message digest as bytes.
40
41 Once ``finalize`` is called this object can no longer be used.
Matthew Iversen505491b2013-10-19 15:56:17 +110042
43 :return bytes: The message digest as bytes.
44
Matthew Iversen505491b2013-10-19 15:56:17 +110045
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050046SHA-1
Matthew Iversen505491b2013-10-19 15:56:17 +110047~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050048
49.. attention::
50
51 NIST has deprecated SHA-1 in favor of the SHA-2 variants. New applications
52 are strongly suggested to use SHA-2 over SHA-1.
53
David Reid1f3d7182013-10-22 16:55:18 -070054.. class:: SHA1()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050055
56 SHA-1 is a cryptographic hash function standardized by NIST. It has a
57 160-bit message digest.
58
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050059SHA-2 Family
Matthew Iversen505491b2013-10-19 15:56:17 +110060~~~~~~~~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050061
David Reid1f3d7182013-10-22 16:55:18 -070062.. class:: SHA224()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050063
64 SHA-224 is a cryptographic hash function from the SHA-2 family and
65 standardized by NIST. It has a 224-bit message digest.
66
David Reid1f3d7182013-10-22 16:55:18 -070067.. class:: SHA256()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050068
69 SHA-256 is a cryptographic hash function from the SHA-2 family and
70 standardized by NIST. It has a 256-bit message digest.
71
David Reid1f3d7182013-10-22 16:55:18 -070072.. class:: SHA384()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050073
74 SHA-384 is a cryptographic hash function from the SHA-2 family and
75 standardized by NIST. It has a 384-bit message digest.
76
David Reid1f3d7182013-10-22 16:55:18 -070077.. class:: SHA512()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050078
79 SHA-512 is a cryptographic hash function from the SHA-2 family and
80 standardized by NIST. It has a 512-bit message digest.
81
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050082RIPEMD160
Matthew Iversen505491b2013-10-19 15:56:17 +110083~~~~~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050084
David Reid1f3d7182013-10-22 16:55:18 -070085.. class:: RIPEMD160()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050086
87 RIPEMD160 is a cryptographic hash function that is part of ISO/IEC
88 10118-3:2004. It has a 160-bit message digest.
89
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050090Whirlpool
Matthew Iversen505491b2013-10-19 15:56:17 +110091~~~~~~~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050092
David Reid1f3d7182013-10-22 16:55:18 -070093.. class:: Whirlpool()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050094
95 Whirlpool is a cryptographic hash function that is part of ISO/IEC
96 10118-3:2004. It has a 512-bit message digest.
97
Paul Kehrer36e7d0d2013-10-18 18:54:40 -050098MD5
Matthew Iversen505491b2013-10-19 15:56:17 +110099~~~
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500100
101.. warning::
102
103 MD5 is a deprecated hash algorithm that has practical known collision
104 attacks. You are strongly discouraged from using it.
105
David Reid1f3d7182013-10-22 16:55:18 -0700106.. class:: MD5()
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500107
Paul Kehrer2b9b3012013-10-22 17:09:38 -0500108 MD5 is a deprecated cryptographic hash function. It has a 128-bit message
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500109 digest and has practical known collision attacks.