def encrypt(self, save_content=False):
if self.encryption_key is None:
raise Exception('Cannot encrypt content, missing encryption key.')
if self.config.get('content') is None:
raise Exception('Cannot encrypt content, content is empty.')
if self.is_encryptable == False:
raise Exception('Cannot encrypt, improper configuration.')
if self.config.get('is_encrypted') == True:
return self.config.get('content')
f = Fernet(self.encryption_key)
if self.config.get('is_binary') == True:
encr_content = f.encrypt(self.config.get('content'))
elif self.config.get('is_binary') == False:
encr_content = f.encrypt(self.config.get('content').encode('utf-8')).decode('utf-8')
else:
raise Exception('Could not tell if file is binary or text. Aborting.')
if save_content == True:
try:
self.config['content'] = encr_content
self.config['content_length'] = len(encr_content)
self.config['is_encrypted'] = True
except:
raise
return encr_content
评论列表
文章目录