python制作钉钉加密/解密工具详细介绍

又是很久没有写技术博客了,盖因最近都在学习知识,也没有总结出什么值得分享的内容,所以一直停笔至今。最近的工作和钉钉的开发打上了交到,官方并没有提供任何#wiki/1514.html” target=”_blank”>python的sdk,于是只能全部自己写。现在我将其中实现起来相对费时间的“加密/解密/签名”部分分享出来,希望能帮助到一些人。加密/解密的具体机制,可以参考 官方文档 。在你的项目中安装这个扩展,可以使用:

pip install dingtalk_crypto

安装。使用方法,可以参考下面的测试代码:

# -*- coding: utf-8 -*-
import json
from dingtalk_crypto import dingtalkcrypto
# 这个是钉钉官方给的测试数据
# @see https://open-doc.dingtalk.com/doc2/detail.htm?tree1a3nbxmcfwkcjvfoq7whjhb+ix3qhpsc9jbadzne1i03peok1laoqorz3+nlygnhwmwj3vdmg’ \
‘+ozrhmeizi7gtrwvdubmfxjz8ej23jvya9vryej5as7xm/zpulx8neqis44w53h1qagnc3przm7zc’ \
‘/d6ibr0rguathb6zrhp8pyrfgnnos9phsbdhlegk+agganfwjxuq9+0pzcy0w9lq==’
crypto = dingtalkcrypto(
‘4g5j64qlyl3zvetqxz5jiocdr586fn2zvjpa8zls3ij’,
‘123456’,
‘suite4xxxxxxxxxxxxxxx’
)
signature = ‘5a65ceeef9aab2d149439f82dc191dd6c5cbe2c0’
timestamp = ‘1445827045067’
nonce = ‘nexhmp4r’
class testcrypto:
def test_decrypt(self):
randstr, length, msg, suite_key = crypto.decrypt(encrypt_text)
msg = json.loads(msg)
assert msg[‘eventtype’] == ‘check_create_suite_url’
assert msg[‘random’] == ‘lpidsnlf’
assert suite_key == ‘suite4xxxxxxxxxxxxxxx’
def test_encode(self):
encrypt_msg = crypto.encrypt(‘hello world’)
randstr, length, msg, suite_key = crypto.decrypt(encrypt_msg)
assert msg == ‘hello world’
def test_check_signature(self):
assert crypto.check_signature(encrypt_text, timestamp, nonce, signature)
def test_sign(self):
msg = crypto.encrypt(‘hello world’)
actual_sig, actual_time, actual_nonce = crypto.sign(msg)
assert true

以上就是python制作钉钉加密/解密工具详细介绍的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论