| 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 |
""" |
| 32 |
MD4 is specified in RFC1320_ and produces the 128 bit digest of a message. |
| 33 |
|
| 34 |
>>> from Crypto.Hash import MD4 |
| 35 |
>>> |
| 36 |
>>> h = MD4.new() |
| 37 |
>>> h.update(b'Hello') |
| 38 |
>>> print h.hexdigest() |
| 39 |
|
| 40 |
MD4 stand for Message Digest version 4, and it was invented by Rivest in 1990. |
| 41 |
This algorithm is insecure. Do not use it for new designs. |
| 42 |
|
| 43 |
.. _RFC1320: http://tools.ietf.org/html/rfc1320 |
| 44 |
""" |
| 45 |
|
| 46 |
from Crypto.Util.py3compat import bord |
| 47 |
|
| 48 |
from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, |
| 49 |
VoidPointer, SmartPointer, |
| 50 |
create_string_buffer, |
| 51 |
get_raw_buffer, c_size_t, |
| 52 |
c_uint8_ptr) |
| 53 |
|
| 54 |
_raw_md4_lib = load_pycryptodome_raw_lib( |
| 55 |
"Crypto.Hash._MD4", |
| 56 |
""" |
| 57 |
int md4_init(void **shaState); |
| 58 |
int md4_destroy(void *shaState); |
| 59 |
int md4_update(void *hs, |
| 60 |
const uint8_t *buf, |
| 61 |
size_t len); |
| 62 |
int md4_digest(const void *shaState, |
| 63 |
uint8_t digest[20]); |
| 64 |
int md4_copy(const void *src, void *dst); |
| 65 |
""") |
| 66 |
|
| 67 |
|
| 68 |
class MD4Hash(object): |
| 69 |
"""Class that implements an MD4 hash |
| 70 |
""" |
| 71 |
|
| 72 |
|
| 73 |
digest_size = 16 |
| 74 |
|
| 75 |
block_size = 64 |
| 76 |
|
| 77 |
oid = "1.2.840.113549.2.4" |
| 78 |
|
| 79 |
def __init__(self, data=None): |
| 80 |
state = VoidPointer() |
| 81 |
result = _raw_md4_lib.md4_init(state.address_of()) |
| 82 |
if result: |
| 83 |
raise ValueError("Error %d while instantiating MD4" |
| 84 |
% result) |
| 85 |
self._state = SmartPointer(state.get(), |
| 86 |
_raw_md4_lib.md4_destroy) |
| 87 |
if data: |
| 88 |
self.update(data) |
| 89 |
|
| 90 |
def update(self, data): |
| 91 |
"""Continue hashing of a message by consuming the next chunk of data. |
| 92 |
|
| 93 |
Repeated calls are equivalent to a single call with the concatenation |
| 94 |
of all the arguments. In other words: |
| 95 |
|
| 96 |
>>> m.update(a); m.update(b) |
| 97 |
|
| 98 |
is equivalent to: |
| 99 |
|
| 100 |
>>> m.update(a+b) |
| 101 |
|
| 102 |
:Parameters: |
| 103 |
data : byte string/byte array/memoryview |
| 104 |
The next chunk of the message being hashed. |
| 105 |
""" |
| 106 |
|
| 107 |
result = _raw_md4_lib.md4_update(self._state.get(), |
| 108 |
c_uint8_ptr(data), |
| 109 |
c_size_t(len(data))) |
| 110 |
if result: |
| 111 |
raise ValueError("Error %d while instantiating MD4" |
| 112 |
% result) |
| 113 |
|
| 114 |
def digest(self): |
| 115 |
"""Return the **binary** (non-printable) digest of the message that |
| 116 |
has been hashed so far. |
| 117 |
|
| 118 |
This method does not change the state of the hash object. |
| 119 |
You can continue updating the object after calling this function. |
| 120 |
|
| 121 |
:Return: A byte string of `digest_size` bytes. It may contain non-ASCII |
| 122 |
characters, including null bytes. |
| 123 |
""" |
| 124 |
|
| 125 |
bfr = create_string_buffer(self.digest_size) |
| 126 |
result = _raw_md4_lib.md4_digest(self._state.get(), |
| 127 |
bfr) |
| 128 |
if result: |
| 129 |
raise ValueError("Error %d while instantiating MD4" |
| 130 |
% result) |
| 131 |
|
| 132 |
return get_raw_buffer(bfr) |
| 133 |
|
| 134 |
def hexdigest(self): |
| 135 |
"""Return the **printable** digest of the message that has been |
| 136 |
hashed so far. |
| 137 |
|
| 138 |
This method does not change the state of the hash object. |
| 139 |
|
| 140 |
:Return: A string of 2* `digest_size` characters. It contains only |
| 141 |
hexadecimal ASCII digits. |
| 142 |
""" |
| 143 |
|
| 144 |
return "".join(["%02x" % bord(x) for x in self.digest()]) |
| 145 |
|
| 146 |
def copy(self): |
| 147 |
"""Return a copy ("clone") of the hash object. |
| 148 |
|
| 149 |
The copy will have the same internal state as the original hash |
| 150 |
object. |
| 151 |
This can be used to efficiently compute the digests of strings that |
| 152 |
share a common initial substring. |
| 153 |
|
| 154 |
:Return: A hash object of the same type |
| 155 |
""" |
| 156 |
|
| 157 |
clone = MD4Hash() |
| 158 |
result = _raw_md4_lib.md4_copy(self._state.get(), |
| 159 |
clone._state.get()) |
| 160 |
if result: |
| 161 |
raise ValueError("Error %d while copying MD4" % result) |
| 162 |
return clone |
| 163 |
|
| 164 |
def new(self, data=None): |
| 165 |
return MD4Hash(data) |
| 166 |
|
| 167 |
|
| 168 |
def new(data=None): |
| 169 |
"""Return a fresh instance of the hash object. |
| 170 |
|
| 171 |
:Parameters: |
| 172 |
data : byte string/byte array/memoryview |
| 173 |
The very first chunk of the message to hash. |
| 174 |
It is equivalent to an early call to `MD4Hash.update()`. |
| 175 |
Optional. |
| 176 |
|
| 177 |
:Return: A `MD4Hash` object |
| 178 |
""" |
| 179 |
return MD4Hash().new(data) |
| 180 |
|
| 181 |
|
| 182 |
digest_size = MD4Hash.digest_size |
| 183 |
|
| 184 |
|
| 185 |
block_size = MD4Hash.block_size |