aes.js 1.1 KB
const moment = require('moment');
const crypto = global.yoho.crypto;
const log = global.yoho.logger;

const connectSymbol = '::::';

/**
 * 生成每日key
 **/
const getDailyKey = () => {
  return `yoho9646${moment().format('YYYYMMDD')}`;
};

/**
 * 密码加密
 **/
const aesPwd = (pwd) => {
  return crypto.encryption('yoho9646yoho9646', pwd);
};

/**
 * uid加密
 **/
const encryptionUid = (uid) => {
  return crypto.encryption('yoho9646abcdefgh', uid + '');
};

/**
 * 动态加密
 **/
const dynamicEncryption = (str) => {
  return crypto.encryption(getDailyKey(), `${str}${connectSymbol}${moment().valueOf()}`);
}

/**
 * 动态解密
 **/
const dynamicDecrypt = (str) => {
  let decryptInfo = {};

  try {
    let decryptStr = crypto.decrypt(getDailyKey(), `${str}`);
    let decryptArr = decryptStr.split(connectSymbol);

    decryptInfo.val = decryptArr[0];
    decryptInfo.timestamp = decryptArr[1];
  } catch (e) {
    log.error(`dynamicDecrypt error on: ${str} , error: ${JSON.stringify(e)}`);
  }

  return decryptInfo;
}

module.exports = {
  aesPwd,
  encryptionUid,
  dynamicEncryption,
  dynamicDecrypt
};