Commits

Jason R. Coombs committed 30f8f99

cipher now passes the tests

Comments (0)

Files changed (3)

ctypescrypto/cipher.py

 	@staticmethod
 	def interpret_type(type):
 		if not isinstance(type, CipherType):
-			if not hasattr(type, __iter__):
+			if not hasattr(type, '__iter__'):
 				type = [type]
-			type = CipherType(*type)
+			type = CipherType.from_name(*type)
 		return type
 
 	def set_padding(self, padding=True):
 		res = evp.CipherUpdate(self, out, out_len, data, len(data))
 		if res != 1:
 			raise CipherError("Error updating cipher")
-		self.out_data.append(out.raw[:out_len])
+		self.out_data.append(out.raw[:out_len.value])
 
-	def final(self, data=None):
+	def finalize(self, data=None):
 		if data is not None:
 			self.update(data)
 		self.finalized = True
 		res = evp.CipherFinal_ex(self, out, out_len)
 		if not res == 1:
 			raise CipherError("Error finalizing cipher")
-		self.out_data.append(out.raw[:out_len])
-		self.final = lambda: ''.join(self.out_data)
+		self.out_data.append(out.raw[:out_len.value])
+		self.finalize = lambda: ''.join(self.out_data)
 		return ''.join(self.out_data)
 
 _init_args = (ctypes.POINTER(Cipher), ctypes.POINTER(CipherType),
 evp.EncryptInit_ex.argtypes = evp.DecryptInit_ex.argtypes = evp.CipherInit_ex.argtypes = _init_args
 evp.EncryptUpdate.argtypes = evp.DecryptUpdate.argtypes = evp.CipherUpdate.argtypes = _update_args
 evp.EncryptFinal_ex.argtypes = evp.DecryptFinal_ex.argtypes = evp.CipherFinal_ex.argtypes = _final_args
-
+evp.CIPHER_CTX_init.argtypes = ctypes.POINTER(Cipher),
 	('final', c_char*MAX_BLOCK_LENGTH),
 ]
 
+CIPHER_CTX_init = lib.EVP_CIPHER_CTX_init
+
 #EncryptInit_ex = lib.EVP_EncryptInit_ex
 #DecryptInit_ex = lib.EVP_DecryptInit_ex
 #...
 from ctypescrypto import cipher
 
 def test_cipher_type():
+	# One can pass the algorithm and mode separately or together
 	t = cipher.CipherType.from_name('AES-256', 'CBC')
+	t = cipher.CipherType.from_name('AES-256-CBC')
 
 def pytest_generate_tests(metafunc):
 	if "data_parts" in metafunc.funcargnames:
 	params = ('AES-256', 'CBC'), key, iv
 	ce = cipher.Cipher(*params)
 	map(ce.update, data_parts)
-	data_enc = ce.finish()
+	data_enc = ce.finalize()
 	cd = cipher.Cipher(*params, encrypt=False)
-	assert cd.finish(data_enc) == ''.join(data_parts)
+	assert cd.finalize(data_enc) == ''.join(data_parts)