X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fdigest.py;h=f1c495c88ae6580c327717fa033227110a70cd97;hb=968dd1b70b51b9df1a5ee3f7c6d1645a536fb7e0;hp=8a9a74e1dbb4d0ddae411ffced1c4dced1298c08;hpb=287e8a5b1d7f5a8129619f733adbfc8b2de3e4c7;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/digest.py b/ctypescrypto/digest.py index 8a9a74e..f1c495c 100644 --- a/ctypescrypto/digest.py +++ b/ctypescrypto/digest.py @@ -15,7 +15,7 @@ algorithm would be available both to this module and hashlib. """ from ctypes import c_int, c_char_p, c_void_p, POINTER, c_long, c_longlong from ctypes import create_string_buffer, byref -from ctypescrypto import libcrypto +from ctypescrypto import libcrypto,pyver, bintype from ctypescrypto.exception import LibCryptoError from ctypescrypto.oid import Oid DIGEST_ALGORITHMS = ("MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512") @@ -56,7 +56,7 @@ class DigestType(object): self.digest_name = digest_name.longname() else: self.digest_name = str(digest_name) - self.digest = libcrypto.EVP_get_digestbyname(self.digest_name) + self.digest = libcrypto.EVP_get_digestbyname(self.digest_name.encode('us-ascii')) if self.digest is None: raise DigestError("Unknown digest: %s" % self.digest_name) @@ -97,7 +97,7 @@ class Digest(object): """ Initializes digest using given type. """ - self.ctx = libcrypto.EVP_MD_CTX_create() + self.ctx = self.newctx() if self.ctx is None: raise DigestError("Unable to create digest context") self.digest_out = None @@ -124,8 +124,8 @@ class Digest(object): """ if self.digest_finalized: raise DigestError("No updates allowed") - if not isinstance(data, str): - raise TypeError("A string is expected") + if not isinstance(data, bintype): + raise TypeError("A byte string is expected") if length is None: length = len(data) elif length > len(data): @@ -167,7 +167,7 @@ class Digest(object): """ try: if self.ctx is not None: - libcrypto.EVP_MD_CTX_destroy(self.ctx) + libcrypto.EVP_MD_CTX_free(self.ctx) del self.ctx except AttributeError: pass @@ -180,21 +180,31 @@ class Digest(object): with hashlib """ from base64 import b16encode - return b16encode(self.digest(data)) + if pyver == 2: + return b16encode(self.digest(data)) + else: + return b16encode(self.digest(data)).decode('us-ascii') # Declare function result and argument types libcrypto.EVP_get_digestbyname.restype = c_void_p libcrypto.EVP_get_digestbyname.argtypes = (c_char_p, ) -libcrypto.EVP_MD_CTX_create.restype = c_void_p +# These two functions are renamed in OpenSSL 1.1.0 +if hasattr(libcrypto,"EVP_MD_CTX_create"): + Digest.newctx = libcrypto.EVP_MD_CTX_create + Digest.freectx = libcrypto.EVP_MD_CTX_destroy +else: + Digest.newctx = libcrypto.EVP_MD_CTX_new + Digest.freectx = libcrypto.EVP_MD_CTX_free +Digest.newctx.restype = c_void_p +Digest.freectx.argtypes = (c_void_p, ) # libcrypto.EVP_MD_CTX_create has no arguments -libcrypto.EVP_DigestInit_ex.restupe = c_int +libcrypto.EVP_DigestInit_ex.restype = c_int libcrypto.EVP_DigestInit_ex.argtypes = (c_void_p, c_void_p, c_void_p) libcrypto.EVP_DigestUpdate.restype = c_int libcrypto.EVP_DigestUpdate.argtypes = (c_void_p, c_char_p, c_longlong) libcrypto.EVP_DigestFinal_ex.restype = c_int libcrypto.EVP_DigestFinal_ex.argtypes = (c_void_p, c_char_p, POINTER(c_long)) -libcrypto.EVP_MD_CTX_destroy.argtypes = (c_void_p, ) libcrypto.EVP_MD_CTX_copy.restype = c_int libcrypto.EVP_MD_CTX_copy.argtypes = (c_void_p, c_void_p) libcrypto.EVP_MD_type.argtypes = (c_void_p, )