mythix 2.8.11 → 2.8.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/crypto-utils.js +96 -1
package/package.json
CHANGED
|
@@ -2,7 +2,42 @@
|
|
|
2
2
|
|
|
3
3
|
/* global Buffer */
|
|
4
4
|
|
|
5
|
-
const { createHash, randomFillSync } = require('crypto');
|
|
5
|
+
const { createHash, randomFillSync } = require('node:crypto');
|
|
6
|
+
|
|
7
|
+
const URL_SAFE_ENCODING_KEYS = { '+': '-', '/': '_', '-': '+', '_': '/' };
|
|
8
|
+
const ENCRYPTION_ALGORITHM = 'aes-256-ctr';
|
|
9
|
+
|
|
10
|
+
function toBase64(_data) {
|
|
11
|
+
var data = _data;
|
|
12
|
+
if (data instanceof Uint8Array)
|
|
13
|
+
data = Buffer.from(data);
|
|
14
|
+
|
|
15
|
+
if (!Buffer.isBuffer(data))
|
|
16
|
+
data = Buffer.from(('' + _data), 'utf8');
|
|
17
|
+
|
|
18
|
+
return data.toString('base64');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function convertBase64ToURLSafe(encodedData) {
|
|
22
|
+
return encodedData.replace(/[+/]/g, (m) => {
|
|
23
|
+
return URL_SAFE_ENCODING_KEYS[m];
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function convertBase64FromURLSafe(encodedData) {
|
|
28
|
+
return encodedData.replace(/[_-]/g, (m) => {
|
|
29
|
+
return URL_SAFE_ENCODING_KEYS[m];
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function toURLSafeBase64(data) {
|
|
34
|
+
return convertBase64ToURLSafe(toBase64(data));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function fromURLSafeBase64(data, encoding) {
|
|
38
|
+
var buffer = Buffer.from(convertBase64FromURLSafe(data), 'base64');
|
|
39
|
+
return (encoding == null) ? buffer : buffer.toString(encoding);
|
|
40
|
+
}
|
|
6
41
|
|
|
7
42
|
function randomBytes(length) {
|
|
8
43
|
let buffer = Buffer.alloc(length);
|
|
@@ -38,7 +73,67 @@ function randomHash(type = 'sha256', length = 128) {
|
|
|
38
73
|
return hash.digest('hex');
|
|
39
74
|
}
|
|
40
75
|
|
|
76
|
+
function hashToken(token, salt) {
|
|
77
|
+
if (!salt)
|
|
78
|
+
throw new TypeError('Utils::hashToken: "salt" can not be empty');
|
|
79
|
+
|
|
80
|
+
// eslint-disable-next-line new-cap
|
|
81
|
+
return SHA512(`${salt}${token}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getSaltProperties(salt) {
|
|
85
|
+
let raw = fromURLSafeBase64(salt, 'utf8');
|
|
86
|
+
let props = JSON.parse(raw);
|
|
87
|
+
return props;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function generateSalt() {
|
|
91
|
+
let props = {
|
|
92
|
+
// eslint-disable-next-line no-magic-numbers
|
|
93
|
+
secretKey: toURLSafeBase64(randomBytes(32)),
|
|
94
|
+
iv: toURLSafeBase64(randomBytes(16)),
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
return toURLSafeBase64(JSON.stringify(props));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// secretKey = 32 chars (base64)
|
|
101
|
+
// iv = 32 chars (base64)
|
|
102
|
+
function encrypt(value, salt) {
|
|
103
|
+
const {
|
|
104
|
+
secretKey,
|
|
105
|
+
iv,
|
|
106
|
+
} = getSaltProperties(salt);
|
|
107
|
+
|
|
108
|
+
const cypher = Crypto.createCipheriv(ENCRYPTION_ALGORITHM, fromURLSafeBase64(secretKey), fromURLSafeBase64(iv));
|
|
109
|
+
const encrypted = Buffer.concat([cypher.update(value), cypher.final()]);
|
|
110
|
+
|
|
111
|
+
return toURLSafeBase64(encrypted);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function decrypt(value, salt) {
|
|
115
|
+
const {
|
|
116
|
+
secretKey,
|
|
117
|
+
iv,
|
|
118
|
+
} = getSaltProperties(salt);
|
|
119
|
+
|
|
120
|
+
const decipher = Crypto.createDecipheriv(ENCRYPTION_ALGORITHM, fromURLSafeBase64(secretKey), fromURLSafeBase64(iv));
|
|
121
|
+
const decrpyted = Buffer.concat([decipher.update(fromURLSafeBase64(value)), decipher.final()]);
|
|
122
|
+
|
|
123
|
+
return decrpyted.toString('utf8');
|
|
124
|
+
}
|
|
125
|
+
|
|
41
126
|
module.exports = {
|
|
127
|
+
toBase64,
|
|
128
|
+
convertBase64ToURLSafe,
|
|
129
|
+
convertBase64FromURLSafe,
|
|
130
|
+
toURLSafeBase64,
|
|
131
|
+
fromURLSafeBase64,
|
|
132
|
+
getSaltProperties,
|
|
133
|
+
generateSalt,
|
|
134
|
+
encrypt,
|
|
135
|
+
decrypt,
|
|
136
|
+
hashToken,
|
|
42
137
|
randomBytes,
|
|
43
138
|
randomHash,
|
|
44
139
|
MD5,
|