python制作加密工具_Python制作钉钉加密/解密工具
又是很久沒有寫技術(shù)博客了,蓋因最近都在學(xué)習(xí)知識(shí),也沒有總結(jié)出什么值得分享的內(nèi)容,所以一直停筆至今。最近的工作和釘釘?shù)拈_發(fā)打上了交到,官方并沒有提供任何Python的SDK,于是只能全部自己寫。現(xiàn)在我將其中實(shí)現(xiàn)起來相對(duì)費(fèi)時(shí)間的“加密/解密/簽名”部分分享出來,希望能幫助到一些人。
加密/解密的具體機(jī)制,可以參考 官方文檔 。
在你的項(xiàng)目中安裝這個(gè)擴(kuò)展,可以使用: pip install dingtalk_crypto安裝。
使用方法,可以參考下面的測(cè)試代碼:
# -*- coding: utf-8 -*-
import json
from dingtalk_crypto import DingTalkCrypto
# 這個(gè)是釘釘官方給的測(cè)試數(shù)據(jù)
# @see https://open-doc.dingtalk.com/doc2/detail.htm?treeId=175&articleId=104945&docType=1#s14
encrypt_text = '1a3NBxmCFwkCJvfoQ7WhJHB+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
最后,貼出項(xiàng)目的 源碼地址 ,希望能一些交流。
總結(jié)
以上是生活随笔為你收集整理的python制作加密工具_Python制作钉钉加密/解密工具的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webform计算某几列结果_利用Dat
- 下一篇: python环境介绍_Python介绍以