config_file.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:cmdb 作者: jonmsawyer 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号