nonebot2/packages/nonebot-adapter-ding/nonebot/adapters/ding/utils.py

16 lines
497 B
Python
Raw Normal View History

import hmac
2020-12-03 00:59:32 +08:00
from nonebot.utils import logger_wrapper
import hashlib
import base64
2020-12-03 00:59:32 +08:00
log = logger_wrapper("DING")
def calc_hmac_base64(timestamp: str, secret: str):
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc,
string_to_sign_enc,
digestmod=hashlib.sha256).digest()
return base64.b64encode(hmac_code)