最近的工作和钉钉的开发打上了交到,官方并没有提供任何nodejs的SDK,于是只能全部自己写。现在我将其中实现起来相对费时间的“加密/解密/签名”部分分享出来,希望能帮助到一些人。
加密/解密的具体机制,可以参考官方文档。
在你的项目中安装这个npm package,可以使用:
npm i dingtalk-encrypt
使用方法,可以参考下面的测试代码。
'use strict';
const CryptoJS = require('crypto-js');
// const AES = require('crypto-js/aes');
const DingTalkEncryptor = require('./DingTalkEncryptor');
const Utils = require('./Utils');
const DingTalkEncryptException = require('./DingTalkEncryptException');
/** 加解密需要用到的token,企业可以随机填写。如 "12345" */
const TOKEN = '666666';
/** 数据加密密钥。用于回调数据的加密,长度固定为43个字符,从a-z, A-Z, 0-9共62个字符中选取,您可以随机生成*/
const ENCODING_AES_KEY = 'TXpRMU5qYzRPVEF4TWpNME5UWTNPRGt3TVRJek5EVTI';
/** 企业corpid, 需要修改成开发者所在企业 */
const CORP_ID = 'ding12345678901234567890123456789012';
const plainText = 'success';
const ENCRYPT_RANDOM_16 = 'aaaabbbbccccdddd';
console.log('\nEncryptor Test:');
const encryptor = new DingTalkEncryptor(TOKEN, ENCODING_AES_KEY, CORP_ID);
console.log(` plainText: ${plainText}, (${plainText.length})`);
const encrypted = encryptor.encrypt(ENCRYPT_RANDOM_16, plainText);
console.log(` encrypted: ${encrypted}`);
const decrypted = encryptor.decrypt(encrypted);
console.log(` decrypted: ${decrypted}, (${decrypted.length})`);
// const timeStamp = (new Date().getTime).toString();
// const nonce = Utils.getRandomStr(8);
const timeStamp = "1561081681688";
const nonce = '88888888';
const sign = encryptor. getSignature(TOKEN, timeStamp, nonce, encrypted);
console.log(`\nSignature Test: \n sign: ${sign}, (${sign.length})`);
const er = new DingTalkEncryptException(900008);
console.log('\nDingTalkEncryptException Test: ');
console.log(' code: '+er.code);
console.log(' message: '+er.message +'\n');
console.log(' error encodingAesKey: ');
new DingTalkEncryptor(TOKEN, "error encodingAesKey", CORP_ID);
最后,贴出项目的源码地址,希望能一些交流。