From 48816308f8c4417a2306ed1fa006473a1332714c Mon Sep 17 00:00:00 2001 From: StarHeartHunt Date: Sat, 3 Jul 2021 13:55:20 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20encryption=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nonebot/adapters/feishu/utils.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 packages/nonebot-adapter-feishu/nonebot/adapters/feishu/utils.py diff --git a/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/utils.py b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/utils.py new file mode 100644 index 00000000..34750255 --- /dev/null +++ b/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/utils.py @@ -0,0 +1,33 @@ +import base64 +import hashlib + +from Crypto.Cipher import AES +from nonebot.utils import logger_wrapper + +log = logger_wrapper("FEISHU") + +class AESCipher(object): + + def __init__(self, key): + self.block_size = AES.block_size + self.key = hashlib.sha256(AESCipher.str_to_bytes(key)).digest() + + @staticmethod + def str_to_bytes(data): + u_type = type(b"".decode('utf8')) + if isinstance(data, u_type): + return data.encode('utf8') + return data + + @staticmethod + def _unpad(s): + return s[:-ord(s[len(s) - 1:])] + + def decrypt(self, enc): + iv = enc[:AES.block_size] + cipher = AES.new(self.key, AES.MODE_CBC, iv) + return self._unpad(cipher.decrypt(enc[AES.block_size:])) + + def decrypt_string(self, enc): + enc = base64.b64decode(enc) + return self.decrypt(enc).decode('utf8')