From 4664108ac1771b694f917b9ef70d358638371c04 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 24 Nov 2013 11:14:27 -0600 Subject: simplify code for wrapping ciphercontext/aeadciphercontext --- cryptography/hazmat/primitives/ciphers/base.py | 34 +++++++++----------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/cryptography/hazmat/primitives/ciphers/base.py b/cryptography/hazmat/primitives/ciphers/base.py index c8c4533b..3f6ca0fe 100644 --- a/cryptography/hazmat/primitives/ciphers/base.py +++ b/cryptography/hazmat/primitives/ciphers/base.py @@ -30,32 +30,22 @@ class Cipher(object): self._backend = backend def encryptor(self): - if isinstance(self.mode, interfaces.ModeWithAAD): - return _AEADCipherContext( - self._backend.create_symmetric_encryption_ctx( - self.algorithm, self.mode - ) - ) - else: - return _CipherContext( - self._backend.create_symmetric_encryption_ctx( - self.algorithm, self.mode - ) - ) + ctx = self._backend.create_symmetric_encryption_ctx( + self.algorithm, self.mode + ) + return self._wrap_ctx(ctx) def decryptor(self): + ctx = self._backend.create_symmetric_decryption_ctx( + self.algorithm, self.mode + ) + return self._wrap_ctx(ctx) + + def _wrap_ctx(self, ctx): if isinstance(self.mode, interfaces.ModeWithAAD): - return _AEADCipherContext( - self._backend.create_symmetric_decryption_ctx( - self.algorithm, self.mode - ) - ) + return _AEADCipherContext(ctx) else: - return _CipherContext( - self._backend.create_symmetric_decryption_ctx( - self.algorithm, self.mode - ) - ) + return _CipherContext(ctx) @utils.register_interface(interfaces.CipherContext) -- cgit v1.2.3