Debian Codegraph @

lib/Crypto/Hash/MD2.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_md2_lib = load_pycryptodome_raw_lib(
40 "Crypto.Hash._MD2",
41 """
42 int md2_init(void **shaState);
43 int md2_destroy(void *shaState);
44 int md2_update(void *hs,
45 const uint8_t *buf,
46 size_t len);
47 int md2_digest(const void *shaState,
48 uint8_t digest[20]);
49 int md2_copy(const void *src, void *dst);
50 """)
51
52
53 class MD2Hash(object):
54 """An MD2 hash object.
55 Do not instantiate directly. Use the :func:`new` function.
56
57 :ivar oid: ASN.1 Object ID
58 :vartype oid: string
59
60 :ivar block_size: the size in bytes of the internal message block,
61 input to the compression function
62 :vartype block_size: integer
63
64 :ivar digest_size: the size in bytes of the resulting hash
65 :vartype digest_size: integer
66 """
67
68 # The size of the resulting hash in bytes.
69 digest_size = 16
70 # The internal block size of the hash algorithm in bytes.
71 block_size = 16
72 # ASN.1 Object ID
73 oid = "1.2.840.113549.2.2"
74
75 def __init__(self, data=None):
76 state = VoidPointer()
77 result = _raw_md2_lib.md2_init(state.address_of())
78 if result:
79 raise ValueError("Error %d while instantiating MD2"
80 % result)
81 self._state = SmartPointer(state.get(),
82 _raw_md2_lib.md2_destroy)
83 if data:
84 self.update(data)
85
86 def update(self, data):
87 """Continue hashing of a message by consuming the next chunk of data.
88
89 Args:
90 data (byte string/byte array/memoryview): The next chunk of the message being hashed.
91 """
92
93 result = _raw_md2_lib.md2_update(self._state.get(),
94 c_uint8_ptr(data),
95 c_size_t(len(data)))
96 if result:
97 raise ValueError("Error %d while instantiating MD2"
98 % result)
99
100 def digest(self):
101 """Return the **binary** (non-printable) digest of the message that has been hashed so far.
102
103 :return: The hash digest, computed over the data processed so far.
104 Binary form.
105 :rtype: byte string
106 """
107
108 bfr = create_string_buffer(self.digest_size)
109 result = _raw_md2_lib.md2_digest(self._state.get(),
110 bfr)
111 if result:
112 raise ValueError("Error %d while instantiating MD2"
113 % result)
114
115 return get_raw_buffer(bfr)
116
117 def hexdigest(self):
118 """Return the **printable** digest of the message that has been hashed so far.
119
120 :return: The hash digest, computed over the data processed so far.
121 Hexadecimal encoded.
122 :rtype: string
123 """
124
125 return "".join(["%02x" % bord(x) for x in self.digest()])
126
127 def copy(self):
128 """Return a copy ("clone") of the hash object.
129
130 The copy will have the same internal state as the original hash
131 object.
132 This can be used to efficiently compute the digests of strings that
133 share a common initial substring.
134
135 :return: A hash object of the same type
136 """
137
138 clone = MD2Hash()
139 result = _raw_md2_lib.md2_copy(self._state.get(),
140 clone._state.get())
141 if result:
142 raise ValueError("Error %d while copying MD2" % result)
143 return clone
144
145 def new(self, data=None):
146 return MD2Hash(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:`MD2Hash.update`.
155 :type data: bytes/bytearray/memoryview
156
157 :Return: A :class:`MD2Hash` hash object
158 """
159
160 return MD2Hash().new(data)
161
162 # The size of the resulting hash in bytes.
163 digest_size = MD2Hash.digest_size
164
165 # The internal block size of the hash algorithm in bytes.
166 block_size = MD2Hash.block_size