]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - ctypescrypto/cipher.py
more style cleanup
[oss/ctypescrypto.git] / ctypescrypto / cipher.py
index 90fd20336aeb80ddb5896009eabdec988bb479d2..a0a9ac33edc3912021a2f826cf04edad660fb788 100644 (file)
@@ -44,7 +44,7 @@ class CipherType:
                """
                self.cipher = libcrypto.EVP_get_cipherbyname(cipher_name)
                if self.cipher is None:
-                       raise CipherError, "Unknown cipher: %s" % cipher_name
+                       raise CipherError("Unknown cipher: %s" % cipher_name)
 
        def __del__(self):
                pass
@@ -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
@@ -115,12 +115,9 @@ class Cipher:
                iv_ptr = c_char_p(iv)
                self.ctx = libcrypto.EVP_CIPHER_CTX_new()
                if self.ctx == 0:
-                       raise CipherError, "Unable to create cipher context"
+                       raise CipherError("Unable to create cipher context")
                self.encrypt = encrypt
-               if encrypt: 
-                       enc = 1
-               else: 
-                       enc = 0
+               enc=1 if encrypt else 0
                if not iv is None and len(iv) != cipher_type.iv_length():
                        raise ValueError("Invalid IV length for this algorithm")
                        
@@ -128,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")
@@ -139,7 +136,7 @@ class Cipher:
                        result = libcrypto.EVP_CipherInit_ex(self.ctx, cipher_type.cipher, None, key_ptr, iv_ptr, c_int(enc))
                if result == 0:
                        self._clean_ctx()
-                       raise CipherError, "Unable to initialize cipher"
+                       raise CipherError("Unable to initialize cipher")
                self.cipher_type = cipher_type
                self.block_size = self.cipher_type.block_size()
                self.cipher_finalized = False
@@ -151,10 +148,7 @@ class Cipher:
                """
                        Sets padding mode of the cipher
                """
-               if padding:
-                       padding_flag = 1
-               else:
-                       padding_flag = 0
+               padding_flag=1 if padding else 0
                libcrypto.EVP_CIPHER_CTX_set_padding(self.ctx, padding_flag)
 
        def update(self, data):
@@ -170,10 +164,10 @@ class Cipher:
                        called
                """
                if self.cipher_finalized :
-                       raise CipherError, "No updates allowed"
-               if type(data) != type(""):
-                       raise TypeError, "A string is expected"
-               if len(data) <= 0:
+                       raise CipherError("No updates allowed")
+               if not isinstance(data,str):
+                       raise TypeError("A string is expected")
+               if len(data) == 0:
                        return ""
                outbuf=create_string_buffer(self.block_size+len(data))
                outlen=c_int(0)
@@ -192,14 +186,14 @@ class Cipher:
                        state, they would be processed and returned.
                """
                if self.cipher_finalized :
-                       raise CipherError, "Cipher operation is already completed"
+                       raise CipherError("Cipher operation is already completed")
                outbuf=create_string_buffer(self.block_size)
                self.cipher_finalized = True
                outlen=c_int(0)
                result = libcrypto.EVP_CipherFinal_ex(self.ctx,outbuf , byref(outlen))
                if result == 0:
                        self._clean_ctx()
-                       raise CipherError, "Unable to finalize cipher"
+                       raise CipherError("Unable to finalize cipher")
                if outlen.value>0:
                        return outbuf.raw[:outlen.value]
                else:
@@ -208,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