From 297667fab5c13886d82b037e74c84459f52e829c Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Thu, 30 Oct 2014 23:25:50 +0300 Subject: [PATCH] Addedet length parameter to Digest.update --- ctypescrypto/digest.py | 14 +++++++++++--- tests/testdigest.py | 42 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/ctypescrypto/digest.py b/ctypescrypto/digest.py index df4e580..0098ab4 100644 --- a/ctypescrypto/digest.py +++ b/ctypescrypto/digest.py @@ -81,15 +81,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" if type(data) != type(""): raise TypeError, "A string is expected" - result = libcrypto.EVP_DigestUpdate(self.ctx, c_char_p(data), len(data)) + 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" diff --git a/tests/testdigest.py b/tests/testdigest.py index 4c4b4ed..e840080 100644 --- a/tests/testdigest.py +++ b/tests/testdigest.py @@ -38,23 +38,52 @@ class TestDigestType(unittest.TestCase): class TestIface(unittest.TestCase): """ Test all methods with one algorithms """ + msg="A quick brown fox jumps over the lazy dog." + dgst="00CFFE7312BF9CA73584F24BDF7DF1D028340397" def test_cons(self): md=digest.DigestType("sha1") dgst=digest.Digest(md) - dgst.update("A quick brown fox jumps over the lazy dog.") + dgst.update(self.msg) self.assertEqual(dgst.digest_size,20) - self.assertEqual(dgst.hexdigest(),"00CFFE7312BF9CA73584F24BDF7DF1D028340397") + self.assertEqual(dgst.hexdigest(),self.dgst) + def test_length(self): + l=len(self.msg) + msg=self.msg+" Dog barks furiously." + dgst=digest.new("sha1") + dgst.update(msg,length=l) + self.assertEqual(dgst.hexdigest(),self.dgst) + def test_badlength(self): + l=len(self.msg) + dgst=digest.new("sha1") + with self.assertRaises(ValueError): + dgst.update(self.msg,length=l+1) def test_bindigest(self): dgst=digest.new("sha1") - dgst.update("A quick brown fox jumps over the lazy dog.") + dgst.update(self.msg) self.assertEqual(dgst.digest_size,20) - self.assertEqual(dgst.digest(),b16decode("00CFFE7312BF9CA73584F24BDF7DF1D028340397",True)) + self.assertEqual(dgst.digest(),b16decode(self.dgst,True)) def test_duplicatedigest(self): dgst=digest.new("sha1") - dgst.update("A quick brown fox jumps over the lazy dog.") + dgst.update(self.msg) v1=dgst.digest() v2=dgst.digest() self.assertEqual(v1,v2) + def test_updatefinalized(self): + dgst=digest.new("sha1") + dgst.update(self.msg) + h=dgst.hexdigest() + with self.assertRaises(digest.DigestError): + dgst.update(self.msg) + def test_wrongtype(self): + dgst=digest.new("sha1") + with self.assertRaises(TypeError): + dgst.update(['a','b','c']) + with self.assertRaises(TypeError): + dgst.update(18) + with self.assertRaises(TypeError): + dgst.update({"a":"b","c":"D"}) + with self.assertRaises(TypeError): + dgst.update(u'\u0430\u0431') def test_copy(self): dgst=digest.new("sha1") dgst.update("A quick brown fox jumps over ") @@ -93,6 +122,9 @@ class TestAlgo(unittest.TestCase): self.assertEqual(d.digest_size,64) d.update("A quick brown fox jumps over the lazy dog.") self.assertEqual(d.hexdigest(),"3045575CF3B873DD656F5F3426E04A4ACD11950BB2538772EE14867002B408E21FF18EF7F7B2CAB484A3C1C0BE3F8ACC4AED536A427353C7748DC365FC1A8646") + def test_wrongdigest(self): + with self.assertRaises(digest.DigestError): + dgst=digest.new("no-such-digest") if __name__ == "__main__": unittest.main() -- 2.39.5