def __del__(self):\r
self._clean_ctx()\r
\r
- def update(self, data):\r
+ def update(self, data, length=None):\r
"""\r
- Hashes given byte string as data\r
+ Hashes given byte string \r
+\r
+ @param data - string to hash\r
+ @param length - if not specifed, entire string is hashed,\r
+ otherwise only first length bytes\r
"""\r
if self.digest_finalized:\r
raise DigestError, "No updates allowed"\r
if type(data) != type(""):\r
raise TypeError, "A string is expected"\r
- result = libcrypto.EVP_DigestUpdate(self.ctx, c_char_p(data), len(data))\r
+ if length is None:\r
+ length=len(data)\r
+ elif length> len(data):\r
+ raise ValueError("Specified length is greater than length of data")\r
+ result = libcrypto.EVP_DigestUpdate(self.ctx, c_char_p(data), length)\r
if result != 1:\r
raise DigestError, "Unable to update digest"\r
\r
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 ")
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()