Debian Codegraph @

lib/Crypto/Hash/SHA224.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # ===================================================================
4 # The contents of this file are dedicated to the public domain. To
5 # the extent that dedication to the public domain is not available,
6 # everyone is granted a worldwide, perpetual, royalty-free,
7 # non-exclusive license to exercise all rights associated with the
8 # contents of this file for any purpose whatsoever.
9 # No rights are reserved.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
15 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 # SOFTWARE.
19 # ===================================================================
20
21 from Crypto.Util.py3compat import bord
22
23 from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
24 VoidPointer, SmartPointer,
25 create_string_buffer,
26 get_raw_buffer, c_size_t,
27 c_uint8_ptr)
28
29 _raw_sha224_lib = load_pycryptodome_raw_lib("Crypto.Hash._SHA224",
30 """
31 int SHA224_init(void **shaState);
32 int SHA224_destroy(void *shaState);
33 int SHA224_update(void *hs,
34 const uint8_t *buf,
35 size_t len);
36 int SHA224_digest(const void *shaState,
37 uint8_t *digest,
38 size_t digest_size);
39 int SHA224_copy(const void *src, void *dst);
40
41 int SHA224_pbkdf2_hmac_assist(const void *inner,
42 const void *outer,
43 const uint8_t *first_digest,
44 uint8_t *final_digest,
45 size_t iterations,
46 size_t digest_size);
47 """)
48
49 class SHA224Hash(object):
50 """A SHA-224 hash object.
51 Do not instantiate directly.
52 Use the :func:`new` function.
53
54 :ivar oid: ASN.1 Object ID
55 :vartype oid: string
56
57 :ivar block_size: the size in bytes of the internal message block,
58 input to the compression function
59 :vartype block_size: integer
60
61 :ivar digest_size: the size in bytes of the resulting hash
62 :vartype digest_size: integer
63 """
64
65 # The size of the resulting hash in bytes.
66 digest_size = 28
67 # The internal block size of the hash algorithm in bytes.
68 block_size = 64
69 # ASN.1 Object ID
70 oid = '2.16.840.1.101.3.4.2.4'
71
72 def __init__(self, data=None):
73 state = VoidPointer()
74 result = _raw_sha224_lib.SHA224_init(state.address_of())
75 if result:
76 raise ValueError("Error %d while instantiating SHA224"
77 % result)
78 self._state = SmartPointer(state.get(),
79 _raw_sha224_lib.SHA224_destroy)
80 if data:
81 self.update(data)
82
83 def update(self, data):
84 """Continue hashing of a message by consuming the next chunk of data.
85
86 Args:
87 data (byte string/byte array/memoryview): The next chunk of the message being hashed.
88 """
89
90 result = _raw_sha224_lib.SHA224_update(self._state.get(),
91 c_uint8_ptr(data),
92 c_size_t(len(data)))
93 if result:
94 raise ValueError("Error %d while hashing data with SHA224"
95 % result)
96
97 def digest(self):
98 """Return the **binary** (non-printable) digest of the message that has been hashed so far.
99
100 :return: The hash digest, computed over the data processed so far.
101 Binary form.
102 :rtype: byte string
103 """
104
105 bfr = create_string_buffer(self.digest_size)
106 result = _raw_sha224_lib.SHA224_digest(self._state.get(),
107 bfr,
108 c_size_t(self.digest_size))
109 if result:
110 raise ValueError("Error %d while making SHA224 digest"
111 % result)
112
113 return get_raw_buffer(bfr)
114
115 def hexdigest(self):
116 """Return the **printable** digest of the message that has been hashed so far.
117
118 :return: The hash digest, computed over the data processed so far.
119 Hexadecimal encoded.
120 :rtype: string
121 """
122
123 return "".join(["%02x" % bord(x) for x in self.digest()])
124
125 def copy(self):
126 """Return a copy ("clone") of the hash object.
127
128 The copy will have the same internal state as the original hash
129 object.
130 This can be used to efficiently compute the digests of strings that
131 share a common initial substring.
132
133 :return: A hash object of the same type
134 """
135
136 clone = SHA224Hash()
137 result = _raw_sha224_lib.SHA224_copy(self._state.get(),
138 clone._state.get())
139 if result:
140 raise ValueError("Error %d while copying SHA224" % result)
141 return clone
142
143 def new(self, data=None):
144 """Create a fresh SHA-224 hash object."""
145
146 return SHA224Hash(data)
147
148
149 def new(data=None):
150 """Create a new hash object.
151
152 :parameter data:
153 Optional. The very first chunk of the message to hash.
154 It is equivalent to an early call to :meth:`SHA224Hash.update`.
155 :type data: byte string/byte array/memoryview
156
157 :Return: A :class:`SHA224Hash` hash object
158 """
159 return SHA224Hash().new(data)
160
161
162 # The size of the resulting hash in bytes.
163 digest_size = SHA224Hash.digest_size
164
165 # The internal block size of the hash algorithm in bytes.
166 block_size = SHA224Hash.block_size
167
168
169 def _pbkdf2_hmac_assist(inner, outer, first_digest, iterations):
170 """Compute the expensive inner loop in PBKDF-HMAC."""
171
172 assert iterations > 0
173
174 bfr = create_string_buffer(len(first_digest));
175 result = _raw_sha224_lib.SHA224_pbkdf2_hmac_assist(
176 inner._state.get(),
177 outer._state.get(),
178 first_digest,
179 bfr,
180 c_size_t(iterations),
181 c_size_t(len(first_digest)))
182
183 if result:
184 raise ValueError("Error %d with PBKDF2-HMAC assist for SHA224" % result)
185
186 return get_raw_buffer(bfr)