Add support for bytearray to hashes

This commit is contained in:
Helder Eijs 2018-03-06 08:39:30 +01:00
parent 2d2f8bf85a
commit db55ee3017
14 changed files with 30 additions and 46 deletions

View file

@ -37,7 +37,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_blake2b_lib = load_pycryptodome_raw_lib("Crypto.Hash._BLAKE2b",
"""
@ -85,11 +85,9 @@ class BLAKE2b_Hash(object):
if digest_bytes in (20, 32, 48, 64) and not key:
self.oid = "1.3.6.1.4.1.1722.12.2.1." + str(digest_bytes)
expect_byte_string(key)
state = VoidPointer()
result = _raw_blake2b_lib.blake2b_init(state.address_of(),
key,
c_char_ptr(key),
c_size_t(len(key)),
c_size_t(digest_bytes)
)
@ -111,9 +109,8 @@ class BLAKE2b_Hash(object):
if self._digest_done and not self._update_after_digest:
raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
expect_byte_string(data)
result = _raw_blake2b_lib.blake2b_update(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while hashing BLAKE2b data" % result)

View file

@ -49,7 +49,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_md4_lib = load_pycryptodome_raw_lib(
"Crypto.Hash._MD4",
@ -104,9 +104,8 @@ class MD4Hash(object):
The next chunk of the message being hashed.
"""
expect_byte_string(data)
result = _raw_md4_lib.md4_update(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while instantiating MD4"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_md5_lib = load_pycryptodome_raw_lib("Crypto.Hash._MD5",
"""
@ -87,10 +87,9 @@ class MD5Hash(object):
data (byte string): The next chunk of the message being hashed.
"""
expect_byte_string(data)
result = _raw_md5_lib.MD5_update(self._state.get(),
data,
c_size_t(len(data)))
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while instantiating MD5"
% result)

View file

@ -34,7 +34,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_ripemd160_lib = load_pycryptodome_raw_lib(
"Crypto.Hash._RIPEMD160",
@ -91,9 +91,8 @@ class RIPEMD160Hash(object):
data (byte string): The next chunk of the message being hashed.
"""
expect_byte_string(data)
result = _raw_ripemd160_lib.ripemd160_update(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while instantiating ripemd160"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_sha224_lib = load_pycryptodome_raw_lib("Crypto.Hash._SHA224",
"""
@ -87,9 +87,8 @@ class SHA224Hash(object):
data (byte string): The next chunk of the message being hashed.
"""
expect_byte_string(data)
result = _raw_sha224_lib.SHA224_update(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while instantiating SHA224"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_sha256_lib = load_pycryptodome_raw_lib("Crypto.Hash._SHA256",
"""
@ -86,9 +86,8 @@ class SHA256Hash(object):
data (byte string): The next chunk of the message being hashed.
"""
expect_byte_string(data)
result = _raw_sha256_lib.SHA256_update(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while instantiating SHA256"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_sha384_lib = load_pycryptodome_raw_lib("Crypto.Hash._SHA384",
"""
@ -86,9 +86,8 @@ class SHA384Hash(object):
data (byte string): The next chunk of the message being hashed.
"""
expect_byte_string(data)
result = _raw_sha384_lib.SHA384_update(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while instantiating SHA384"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
from Crypto.Hash.keccak import _raw_keccak_lib
@ -72,9 +72,8 @@ class SHA3_224_Hash(object):
if self._digest_done and not self._update_after_digest:
raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
expect_byte_string(data)
result = _raw_keccak_lib.keccak_absorb(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while updating SHA-3/224"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
from Crypto.Hash.keccak import _raw_keccak_lib
@ -72,9 +72,8 @@ class SHA3_256_Hash(object):
if self._digest_done and not self._update_after_digest:
raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
expect_byte_string(data)
result = _raw_keccak_lib.keccak_absorb(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while updating SHA-3/256"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
from Crypto.Hash.keccak import _raw_keccak_lib
@ -72,9 +72,8 @@ class SHA3_384_Hash(object):
if self._digest_done and not self._update_after_digest:
raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
expect_byte_string(data)
result = _raw_keccak_lib.keccak_absorb(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while updating SHA-3/384"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
from Crypto.Hash.keccak import _raw_keccak_lib
@ -72,9 +72,8 @@ class SHA3_512_Hash(object):
if self._digest_done and not self._update_after_digest:
raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
expect_byte_string(data)
result = _raw_keccak_lib.keccak_absorb(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while updating SHA-3/512"

View file

@ -24,7 +24,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_sha512_lib = load_pycryptodome_raw_lib("Crypto.Hash._SHA512",
"""
@ -86,9 +86,8 @@ class SHA512Hash(object):
data (byte string): The next chunk of the message being hashed.
"""
expect_byte_string(data)
result = _raw_sha512_lib.SHA512_update(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while instantiating SHA512"

View file

@ -34,7 +34,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
from Crypto.Hash.keccak import _raw_keccak_lib
@ -74,9 +74,8 @@ class SHAKE256_XOF(object):
if self._is_squeezing:
raise TypeError("You cannot call 'update' after the first 'read'")
expect_byte_string(data)
result = _raw_keccak_lib.keccak_absorb(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while updating SHAKE256 state"

View file

@ -34,7 +34,7 @@ from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
expect_byte_string)
c_char_ptr)
_raw_keccak_lib = load_pycryptodome_raw_lib("Crypto.Hash._keccak",
"""
@ -88,9 +88,8 @@ class Keccak_Hash(object):
if self._digest_done and not self._update_after_digest:
raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
expect_byte_string(data)
result = _raw_keccak_lib.keccak_absorb(self._state.get(),
data,
c_char_ptr(data),
c_size_t(len(data)))
if result:
raise ValueError("Error %d while updating keccak" % result)