From: Victor Wagner Date: Sun, 21 Dec 2014 10:23:51 +0000 (+0300) Subject: more style cleanup X-Git-Url: https://www.wagner.pp.ru/gitweb/?p=oss%2Fctypescrypto.git;a=commitdiff_plain;h=ec92bf04b008b0401014cae90fa2dfca18efa02c more style cleanup --- diff --git a/ctypescrypto/cipher.py b/ctypescrypto/cipher.py index ff61478..a0a9ac3 100644 --- a/ctypescrypto/cipher.py +++ b/ctypescrypto/cipher.py @@ -79,7 +79,7 @@ class CipherType: """ Return cipher's algorithm name, derived from OID """ - return self.oid().short_name() + return self.oid().shortname() def oid(self): """ Returns ASN.1 object identifier of the cipher as @@ -125,7 +125,7 @@ class Cipher: if (cipher_type.flags() & 8) != 0: # Variable key length cipher. result = libcrypto.EVP_CipherInit_ex(self.ctx, cipher_type.cipher, None, None, None, c_int(enc)) - result=libcrypto.EVP_CIPHER_CTX_set_key_length(self.ctx,len(key)) + result = libcrypto.EVP_CIPHER_CTX_set_key_length(self.ctx,len(key)) if result == 0: self._clean_ctx() raise CipherError("Unable to set key length") @@ -202,8 +202,8 @@ class Cipher: def _clean_ctx(self): try: if self.ctx is not None: - self.libcrypto.EVP_CIPHER_CTX_cleanup(self.ctx) - self.libcrypto.EVP_CIPHER_CTX_free(self.ctx) + libcrypto.EVP_CIPHER_CTX_cleanup(self.ctx) + libcrypto.EVP_CIPHER_CTX_free(self.ctx) del(self.ctx) except AttributeError: pass diff --git a/tests/testcipher.py b/tests/testcipher.py index cb59802..7fa5a97 100644 --- a/tests/testcipher.py +++ b/tests/testcipher.py @@ -10,6 +10,7 @@ class TestCipherType(unittest.TestCase): self.assertEqual(ct.block_size(),8) self.assertEqual(ct.key_length(),8) self.assertEqual(ct.iv_length(),8) + self.assertEqual(ct.algo(),'DES-CBC') self.assertEqual(ct.oid().shortname(),"DES-CBC") self.assertEqual(ct.mode(),"CBC") def test_ciphaesofb(self): @@ -17,10 +18,18 @@ class TestCipherType(unittest.TestCase): self.assertEqual(ct.block_size(),1) self.assertEqual(ct.key_length(),32) self.assertEqual(ct.iv_length(),16) + self.assertEqual(ct.algo(),'AES-256-OFB') self.assertEqual(ct.oid().shortname(),"AES-256-OFB") self.assertEqual(ct.mode(),"OFB") + def test_unknowncipher(self): + with self.assertRaises(cipher.CipherError): + ct=cipher.CipherType("no-such-cipher") class TestEncryptDecrypt(unittest.TestCase): + def test_cons_nokey(self): + ct=cipher.CipherType("DES-CBC") + with self.assertRaises(ValueError): + c=cipher.Cipher(ct,None,None) def test_blockcipher(self): data="sdfdsddf" key='abcdabcd' @@ -79,7 +88,7 @@ class TestEncryptDecrypt(unittest.TestCase): with self.assertRaises(ValueError): c=cipher.new("AES-128-OFB",key,iv=iv) def test_variable_keylength(self): - encryptkey="abcdefabcdef" + encryptkey="abcdefabcdefghtlgvasdgdgsdgdsg" data="asdfsdfsdfsdfsdfsdfsdfsdf" iv="abcdefgh" c=cipher.new("bf-ofb",encryptkey,iv=iv) @@ -88,5 +97,6 @@ class TestEncryptDecrypt(unittest.TestCase): 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()