back-helper.js 945 Bytes
/**
 * Created by TaoHuang on 2016/6/27.
 */

'use strict';


/**
 * 签名算法参考微信支付加密算法
 * 参考链接:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
 *
 */


const SALT = "_+@#$%^";

/**
 * 生成加密token
 * @param data json
 * @returns string
 */
const makeToken = (data) => {
    let saltData = Object.assign(data, {key: this.SALT});

    let str = this._packageObject(saltData);

    return this._encodeMD5(str);
};

const validateToken = (data, token) => {
    let saltData = Object.assign(data, {key: this.SALT});

    let str = this._packageObject(saltData);

    return this._encodeMD5(str) === token;
};

const _packageObject = (data) => {
    return _.keys(data).sort().map(key => `${key}=${data[key]}`).join('&').toUpperCase();
};

const _encodeMD5 = (str) => {
    const md5 = require('md5');
    return md5(str).toUpperCase();
};

module.exports = {
    makeToken,
    validateToken
};