forked from elixirChain/dingtalk-encrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncryptTest.js
49 lines (43 loc) · 2.07 KB
/
EncryptTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
const DingTalkEncryptor = require('./DingTalkEncryptor');
const utils = require('./Utils');
// const DingTalkEncryptException = require('./DingTalkEncryptException');
/** 加解密需要,可以随机填写。如 "12345" */
const TOKEN = '666666';
/** 加密密钥,用于回调数据的加密,固定为43个字符,从[a-z, A-Z, 0-9]共62个字符中随机生成*/
const ENCODING_AES_KEY = 'TXpRMU5qYzRPVEF4TWpNME5UWTNPRGt3TVRJek5EVTI';
// const ENCODING_AES_KEY = utils.getRandomStr(43);
console.log('ENCODING_AES_KEY:\n' + ENCODING_AES_KEY);
/** 企业corpid, 可以在钉钉企业管理后台查看(https://oa.dingtalk.com/) */
const CORP_ID = 'ding12345678901234567890123456789012';
/** 实例化加密类 */
console.log('\nEncryptor Test:');
const encryptor = new DingTalkEncryptor(TOKEN, ENCODING_AES_KEY, CORP_ID);
// const plainText = 'success';
const ENCRYPT_RANDOM_16 = 'aaaabbbbccccdddd';
// const timeStamp = (new Date().getTime()).toString();
// const nonce = utils.getRandomStr(8);
const timeStamp = '1561081681688';
const nonce = '88888888';
/** 测试加解密响应报文或者字符串 */
// const testJson = {
// EventType: 'bpms_instance_change',
// processInstanceId: 'ad253df6-e175caf-68085c60ba8a',
// corpId: 'ding2c4d8175651',
// createTime: 1495592259000,
// title: '自测-1016',
// type: 'start',
// staffId: 'er5875',
// url: 'https://aflow.dingtalk.com/dingtalk/mobile/homepage.htm',
// processCode: 'xxx',
// };
const testJson = '中文乱码测试/abc/123';
// console.log(JSON.parse(JSON.stringify(testJson)));
// const unencryptedJson = 'success';
const unencryptedJson = JSON.stringify(testJson);
console.log(` node unencryptedJson:\n ${unencryptedJson}`);
const encryptedJson = encryptor.encrypt(ENCRYPT_RANDOM_16, unencryptedJson);
console.log(` \nnode encryptedJson:\n ${encryptedJson}`);
const decryptedJson = encryptor.decrypt(encryptedJson);
console.log(` \nnode decryptedJson:\n ${decryptedJson}, (${decryptedJson.length})`);
console.log(' \nnode sign:\n ' + encryptor.getSignature(TOKEN, timeStamp, nonce, encryptedJson));