| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 69 |
digest_size = 16 |
| 70 |
|
| 71 |
block_size = 16 |
| 72 |
|
| 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 |
|
| 163 |
digest_size = MD2Hash.digest_size |
| 164 |
|
| 165 |
|
| 166 |
block_size = MD2Hash.block_size |