]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - tests/testcipher.py
cleaned up documentation
[oss/ctypescrypto.git] / tests / testcipher.py
index 052193c2143062046ed8ae615b03874486622523..3cc848c0a963600841d271c59d0cd6916d924f3a 100644 (file)
@@ -1,6 +1,7 @@
 from ctypescrypto.oid import Oid
 from ctypescrypto import cipher
 import unittest
+import sys
 
 
 class TestCipherType(unittest.TestCase):
@@ -56,13 +57,38 @@ class TestEncryptDecrypt(unittest.TestCase):
 
        def test_ofb_noiv(self):
                data="sdfdsddfxx"
-               key='abcdabcd'
-               c=cipher.new("AES-256-OFB",key)
+               encryptkey='abcdabcd'*4
+               decryptkey=encryptkey[0:5]+encryptkey[5:]
+
+
+               c=cipher.new("AES-256-OFB",encryptkey)
                enc=c.update(data)+c.finish()
                # See if padding is added by default
                self.assertEqual(len(enc),len(data))
-               d=cipher.new("AES-256-OFB",key,encrypt=False)
+               d=cipher.new("AES-256-OFB",decryptkey,encrypt=False)
                dec=d.update(enc)+d.finish()
                self.assertEqual(data,dec)
+       def test_wrong_keylength(self):
+               data="sdfsdfxxx"
+               key="abcdabcd"
+               with self.assertRaises(ValueError):
+                       c=cipher.new("AES-128-OFB",key)
+       def test_wrong_ivlength(self):
+               key="abcdabcdabcdabcd"
+               iv="xxxxx"
+               with self.assertRaises(ValueError):
+                       c=cipher.new("AES-128-OFB",key,iv=iv)
+       def test_variable_keylength(self):
+               encryptkey="abcdefabcdef"
+               data="asdfsdfsdfsdfsdfsdfsdfsdf"
+               iv="abcdefgh"
+               c=cipher.new("bf-ofb",encryptkey,iv=iv)
+               ciphertext=c.update(data)+c.finish()
+               decryptkey=encryptkey[0:5]+encryptkey[5:]
+               with open("cipher.txt","w") as f:
+                       f.write(repr(ciphertext)+"\n")
+               d=cipher.new("bf-ofb",decryptkey,encrypt=False,iv=iv)
+               deciph=d.update(ciphertext)+d.finish()
+               self.assertEqual(deciph,data)
 if __name__ == '__main__':
        unittest.main()