Giter Club home page Giter Club logo

dingtalk-encrypt's Introduction

dingtalk-encrypt

DingTalk Encrypt Node Version.
Refer to Java version:

Issues: It's your turn!

Usage

this repository

  • git clone
  • npm install
  • run 'EncryptTest.js' for main APIs.

npm module

  • npm install --save dingtalk-encrypt
  • use APIs as follows API Doc.

API Doc

  • Need constants:

TOKEN - Random string for signature, unrestricted, such as "123456".
ENCODING_AES_KEY - Secret key for callback data, random 43 characters of [a-z, A-Z, 0-9].
CORP_ID - DingTalk corpId from the Official OA.

  • Main APIs(Usage Example):

    • getEncryptedMap
    • getDecryptMsg
    • getSignature
    • encrypt
    • decrypt
  • Example

    • 处理钉钉回调
    // 参考:钉钉开发文档-业务事件回调 
    const DingTalkEncryptor = require('dingtalk-encrypt');
    const utils = require('dingtalk-encrypt/Utils');
    /** 加解密需要,可以随机填写。如 "12345" */
    const TOKEN = '666666';
    /** 加密密钥,用于回调数据的加密,固定为43个字符,从[a-z, A-Z, 0-9]共62个字符中随机生成*/
    const ENCODING_AES_KEY = 'TXpRMU5qYzRPVEF4TWpNME5UWTNPRGt3TVRJek5EVTI';
    // const ENCODING_AES_KEY = utils.getRandomStr(43);
    /** 企业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 = encryptor.getDecryptMsg(signature, timestamp, nonce, encryptMsg);
    console.log('DEBUG plainText: ' + plainText);
    const obj = JSON.parse(plainText);
    // 回调事件类型,根据事件类型和业务数据处理相应业务
    const eventType = obj.EventType;
    // 响应数据:加密'success',签名等等
    encryptor.getEncryptedMap('success', timestamp, utils.getRandomStr(8));
    
    • 单独使用加/解密
    /** 测试加解密响应报文或者字符串 */
    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',
    };
    // 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));
    

Thanks To

dingtalk-encrypt's People

Contributors

senique avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.