Debian Codegraph @

lib/Crypto/Hash/RIPEMD160.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2014, Legrandin <[email protected]>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 # ===================================================================
30
31 from Crypto.Util.py3compat import bord
32
33 from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
34 VoidPointer, SmartPointer,
35 create_string_buffer,
36 get_raw_buffer, c_size_t,
37 c_uint8_ptr)
38
39 _raw_ripemd160_lib = load_pycryptodome_raw_lib(
40 "Crypto.Hash._RIPEMD160",
41 """
42 int ripemd160_init(void **shaState);
43 int ripemd160_destroy(void *shaState);
44 int ripemd160_update(void *hs,
45 const uint8_t *buf,
46 size_t len);
47 int ripemd160_digest(const void *shaState,
48 uint8_t digest[20]);
49 int ripemd160_copy(const void *src, void *dst);
50 """)
51
52
53 class RIPEMD160Hash(object):
54 """A RIPEMD-160 hash object.
55 Do not instantiate directly.
56 Use the :func:`new` function.
57
58 :ivar oid: ASN.1 Object ID
59 :vartype oid: string
60
61 :ivar block_size: the size in bytes of the internal message block,
62 input to the compression function
63 :vartype block_size: integer
64
65 :ivar digest_size: the size in bytes of the resulting hash
66 :vartype digest_size: integer
67 """
68
69 # The size of the resulting hash in bytes.
70 digest_size = 20
71 # The internal block size of the hash algorithm in bytes.
72 block_size = 64
73 # ASN.1 Object ID
74 oid = "1.3.36.3.2.1"
75
76 def __init__(self, data=None):
77 state = VoidPointer()
78 result = _raw_ripemd160_lib.ripemd160_init(state.address_of())
79 if result:
80 raise ValueError("Error %d while instantiating RIPEMD160"
81 % result)
82 self._state = SmartPointer(state.get(),
83 _raw_ripemd160_lib.ripemd160_destroy)
84 if data:
85 self.update(data)
86
87 def update(self, data):
88 """Continue hashing of a message by consuming the next chunk of data.
89
90 Args:
91 data (byte string/byte array/memoryview): The next chunk of the message being hashed.
92 """
93
94 result = _raw_ripemd160_lib.ripemd160_update(self._state.get(),
95 c_uint8_ptr(data),
96 c_size_t(len(data)))
97 if result:
98 raise ValueError("Error %d while instantiating ripemd160"
99 % result)
100
101 def digest(self):
102 """Return the **binary** (non-printable) digest of the message that has been hashed so far.
103
104 :return: The hash digest, computed over the data processed so far.
105 Binary form.
106 :rtype: byte string
107 """
108
109 bfr = create_string_buffer(self.digest_size)
110 result = _raw_ripemd160_lib.ripemd160_digest(self._state.get(),
111 bfr)
112 if result:
113 raise ValueError("Error %d while instantiating ripemd160"
114 % result)
115
116 return get_raw_buffer(bfr)
117
118 def hexdigest(self):
119 """Return the **printable** digest of the message that has been hashed so far.
120
121 :return: The hash digest, computed over the data processed so far.
122 Hexadecimal encoded.
123 :rtype: string
124 """
125
126 return "".join(["%02x" % bord(x) for x in self.digest()])
127
128 def copy(self):
129 """Return a copy ("clone") of the hash object.
130
131 The copy will have the same internal state as the original hash
132 object.
133 This can be used to efficiently compute the digests of strings that
134 share a common initial substring.
135
136 :return: A hash object of the same type
137 """
138
139 clone = RIPEMD160Hash()
140 result = _raw_ripemd160_lib.ripemd160_copy(self._state.get(),
141 clone._state.get())
142 if result:
143 raise ValueError("Error %d while copying ripemd160" % result)
144 return clone
145
146 def new(self, data=None):
147 """Create a fresh RIPEMD-160 hash object."""
148
149 return RIPEMD160Hash(data)
150
151
152 def new(data=None):
153 """Create a new hash object.
154
155 :parameter data:
156 Optional. The very first chunk of the message to hash.
157 It is equivalent to an early call to :meth:`RIPEMD160Hash.update`.
158 :type data: byte string/byte array/memoryview
159
160 :Return: A :class:`RIPEMD160Hash` hash object
161 """
162
163 return RIPEMD160Hash().new(data)
164
165 # The size of the resulting hash in bytes.
166 digest_size = RIPEMD160Hash.digest_size
167
168 # The internal block size of the hash algorithm in bytes.
169 block_size = RIPEMD160Hash.block_size