aes.js
1.1 KB
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
49
50
51
52
53
54
55
56
57
58
59
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
};