X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ctypescrypto%2Fdigest.py;h=26b46bd7337e5bbd467a8252ec29514775a5edd9;hb=0032b97ea6d4348c0f11ee160c824ebe2e181696;hp=4e33d92bb3e147b28a05fd22dd18ef315078dff2;hpb=22338425226b926b97b2e821e4d7d2ea9b2f1b88;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/digest.py b/ctypescrypto/digest.py index 4e33d92..26b46bd 100644 --- a/ctypescrypto/digest.py +++ b/ctypescrypto/digest.py @@ -1,5 +1,6 @@ """ - Implmenets interface to OpenSSL EVP_Digest* functions. + Implements interface to OpenSSL EVP_Digest* functions. + Interface made as close to hashlib as possible. This module is really an excess effort. Hashlib allows access to @@ -18,6 +19,7 @@ from ctypescrypto.exception import LibCryptoError from ctypescrypto.oid import Oid DIGEST_ALGORITHMS = ("MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512") +__all__ = ['DigestError','Digest','DigestType','new'] class DigestError(LibCryptoError): pass @@ -44,7 +46,7 @@ class DigestType: self.digest_name = digest_name self.digest = libcrypto.EVP_get_digestbyname(self.digest_name) if self.digest is None: - raise DigestError, "Unknown digest: %s" % self.digest_name + raise DigestError("Unknown digest: %s" % self.digest_name) def __del__(self): pass @@ -68,11 +70,11 @@ class Digest: self._clean_ctx() self.ctx = libcrypto.EVP_MD_CTX_create() if self.ctx == 0: - raise DigestError, "Unable to create digest context" + raise DigestError("Unable to create digest context") result = libcrypto.EVP_DigestInit_ex(self.ctx, digest_type.digest, None) if result == 0: self._clean_ctx() - raise DigestError, "Unable to initialize digest" + raise DigestError("Unable to initialize digest") self.digest_type = digest_type self.digest_size = self.digest_type.digest_size() self.block_size = self.digest_type.block_size() @@ -80,15 +82,23 @@ class Digest: def __del__(self): self._clean_ctx() - def update(self, data): + def update(self, data, length=None): """ - Hashes given byte string as data + Hashes given byte string + + @param data - string to hash + @param length - if not specifed, entire string is hashed, + otherwise only first length bytes """ if self.digest_finalized: - raise DigestError, "No updates allowed" + raise DigestError("No updates allowed") if type(data) != type(""): - raise TypeError, "A string is expected" - result = libcrypto.EVP_DigestUpdate(self.ctx, c_char_p(data), len(data)) + raise TypeError("A string is expected") + if length is None: + length=len(data) + elif length> len(data): + raise ValueError("Specified length is greater than length of data") + result = libcrypto.EVP_DigestUpdate(self.ctx, c_char_p(data), length) if result != 1: raise DigestError, "Unable to update digest" @@ -105,7 +115,7 @@ class Digest: length = c_long(0) result = libcrypto.EVP_DigestFinal_ex(self.ctx, self.digest_out, byref(length)) if result != 1 : - raise DigestError, "Unable to finalize digest" + raise DigestError("Unable to finalize digest") self.digest_finalized = True return self.digest_out.raw[:self.digest_size] def copy(self):