Python製作DingTalk加密/解密工具,python解密

來源:互聯網
上載者:User

Python製作DingTalk加密/解密工具,python解密

又是很久沒有寫技術部落格了,蓋因最近都在學習知識,也沒有總結出什麼值得分享的內容,所以一直停筆至今。最近的工作和DingTalk的開發打上了交到,官方並沒有提供任何Python的SDK,於是只能全部自己寫。現在我將其中實現起來相對費時間的“加密/解密/簽名”部分分享出來,希望能協助到一些人。

加密/解密的具體機制,可以參考 官方文檔 。

在你的項目中安裝這個擴充,可以使用: pip install dingtalk_crypto 安裝。

使用方法,可以參考下面的測試代碼:

# -*- coding: utf-8 -*-import jsonfrom dingtalk_crypto import DingTalkCrypto# 這個是DingTalk官方給的測試資料# @see https://open-doc.dingtalk.com/doc2/detail.htm?treeId=175&articleId=104945&docType=1#s14encrypt_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

最後,貼出項目的 源碼地址 ,希望能一些交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.