cc: format encryptor.py with black

This commit is contained in:
Mike Salvatore 2021-02-11 20:17:22 -05:00
parent 3f6c268f40
commit d265238107
1 changed files with 10 additions and 7 deletions

View File

@ -25,30 +25,33 @@ class Encryptor:
def _init_key(self, password_file):
self._cipher_key = Random.new().read(self._BLOCK_SIZE)
with open(password_file, 'wb') as f:
with open(password_file, "wb") as f:
f.write(self._cipher_key)
def _load_existing_key(self, password_file):
with open(password_file, 'rb') as f:
with open(password_file, "rb") as f:
self._cipher_key = f.read()
def _pad(self, message):
return message + (self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE)) * chr(
self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE))
self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE)
)
def _unpad(self, message: str):
return message[0:-ord(message[len(message) - 1])]
return message[0 : -ord(message[len(message) - 1])]
def enc(self, message: str):
cipher_iv = Random.new().read(AES.block_size)
cipher = AES.new(self._cipher_key, AES.MODE_CBC, cipher_iv)
return base64.b64encode(cipher_iv + cipher.encrypt(self._pad(message).encode())).decode()
return base64.b64encode(
cipher_iv + cipher.encrypt(self._pad(message).encode())
).decode()
def dec(self, enc_message):
enc_message = base64.b64decode(enc_message)
cipher_iv = enc_message[0:AES.block_size]
cipher_iv = enc_message[0 : AES.block_size]
cipher = AES.new(self._cipher_key, AES.MODE_CBC, cipher_iv)
return self._unpad(cipher.decrypt(enc_message[AES.block_size:]).decode())
return self._unpad(cipher.decrypt(enc_message[AES.block_size :]).decode())
def initialize_encryptor(data_dir):