| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from Crypto.Util.py3compat import * |
| 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_sha1_lib = load_pycryptodome_raw_lib("Crypto.Hash._SHA1", |
| 30 |
""" |
| 31 |
#define SHA1_DIGEST_SIZE 20 |
| 32 |
|
| 33 |
int SHA1_init(void **shaState); |
| 34 |
int SHA1_destroy(void *shaState); |
| 35 |
int SHA1_update(void *hs, |
| 36 |
const uint8_t *buf, |
| 37 |
size_t len); |
| 38 |
int SHA1_digest(const void *shaState, |
| 39 |
uint8_t digest[SHA1_DIGEST_SIZE]); |
| 40 |
int SHA1_copy(const void *src, void *dst); |
| 41 |
|
| 42 |
int SHA1_pbkdf2_hmac_assist(const void *inner, |
| 43 |
const void *outer, |
| 44 |
const uint8_t first_digest[SHA1_DIGEST_SIZE], |
| 45 |
uint8_t final_digest[SHA1_DIGEST_SIZE], |
| 46 |
size_t iterations); |
| 47 |
""") |
| 48 |
|
| 49 |
class SHA1Hash(object): |
| 50 |
"""A SHA-1 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 |
|
| 66 |
digest_size = 20 |
| 67 |
|
| 68 |
block_size = 64 |
| 69 |
|
| 70 |
oid = "1.3.14.3.2.26" |
| 71 |
|
| 72 |
def __init__(self, data=None): |
| 73 |
state = VoidPointer() |
| 74 |
result = _raw_sha1_lib.SHA1_init(state.address_of()) |
| 75 |
if result: |
| 76 |
raise ValueError("Error %d while instantiating SHA1" |
| 77 |
% result) |
| 78 |
self._state = SmartPointer(state.get(), |
| 79 |
_raw_sha1_lib.SHA1_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_sha1_lib.SHA1_update(self._state.get(), |
| 91 |
c_uint8_ptr(data), |
| 92 |
c_size_t(len(data))) |
| 93 |
if result: |
| 94 |
raise ValueError("Error %d while instantiating SHA1" |
| 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_sha1_lib.SHA1_digest(self._state.get(), |
| 107 |
bfr) |
| 108 |
if result: |
| 109 |
raise ValueError("Error %d while instantiating SHA1" |
| 110 |
% result) |
| 111 |
|
| 112 |
return get_raw_buffer(bfr) |
| 113 |
|
| 114 |
def hexdigest(self): |
| 115 |
"""Return the **printable** digest of the message that has been hashed so far. |
| 116 |
|
| 117 |
:return: The hash digest, computed over the data processed so far. |
| 118 |
Hexadecimal encoded. |
| 119 |
:rtype: string |
| 120 |
""" |
| 121 |
|
| 122 |
return "".join(["%02x" % bord(x) for x in self.digest()]) |
| 123 |
|
| 124 |
def copy(self): |
| 125 |
"""Return a copy ("clone") of the hash object. |
| 126 |
|
| 127 |
The copy will have the same internal state as the original hash |
| 128 |
object. |
| 129 |
This can be used to efficiently compute the digests of strings that |
| 130 |
share a common initial substring. |
| 131 |
|
| 132 |
:return: A hash object of the same type |
| 133 |
""" |
| 134 |
|
| 135 |
clone = SHA1Hash() |
| 136 |
result = _raw_sha1_lib.SHA1_copy(self._state.get(), |
| 137 |
clone._state.get()) |
| 138 |
if result: |
| 139 |
raise ValueError("Error %d while copying SHA1" % result) |
| 140 |
return clone |
| 141 |
|
| 142 |
def new(self, data=None): |
| 143 |
"""Create a fresh SHA-1 hash object.""" |
| 144 |
|
| 145 |
return SHA1Hash(data) |
| 146 |
|
| 147 |
|
| 148 |
def new(data=None): |
| 149 |
"""Create a new hash object. |
| 150 |
|
| 151 |
:parameter data: |
| 152 |
Optional. The very first chunk of the message to hash. |
| 153 |
It is equivalent to an early call to :meth:`SHA1Hash.update`. |
| 154 |
:type data: byte string/byte array/memoryview |
| 155 |
|
| 156 |
:Return: A :class:`SHA1Hash` hash object |
| 157 |
""" |
| 158 |
return SHA1Hash().new(data) |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
digest_size = SHA1Hash.digest_size |
| 163 |
|
| 164 |
|
| 165 |
block_size = SHA1Hash.block_size |
| 166 |
|
| 167 |
|
| 168 |
def _pbkdf2_hmac_assist(inner, outer, first_digest, iterations): |
| 169 |
"""Compute the expensive inner loop in PBKDF-HMAC.""" |
| 170 |
|
| 171 |
assert len(first_digest) == digest_size |
| 172 |
assert iterations > 0 |
| 173 |
|
| 174 |
bfr = create_string_buffer(digest_size); |
| 175 |
result = _raw_sha1_lib.SHA1_pbkdf2_hmac_assist( |
| 176 |
inner._state.get(), |
| 177 |
outer._state.get(), |
| 178 |
first_digest, |
| 179 |
bfr, |
| 180 |
c_size_t(iterations)) |
| 181 |
|
| 182 |
if result: |
| 183 |
raise ValueError("Error %d with PBKDF2-HMAC assis for SHA1" % result) |
| 184 |
|
| 185 |
return get_raw_buffer(bfr) |