ayiin 2.0.50 → 2.0.51
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/dist/methods/crypt.d.ts +6 -20
- package/dist/methods/crypt.js +79 -33
- package/package.json +1 -1
package/dist/methods/crypt.d.ts
CHANGED
|
@@ -1,25 +1,11 @@
|
|
|
1
1
|
declare class Crypt {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @returns {Buffer} - The encrypted or decrypted data as a Buffer.
|
|
7
|
-
*/
|
|
2
|
+
private chars;
|
|
3
|
+
private base62Encode;
|
|
4
|
+
private base62Decode;
|
|
5
|
+
private generateKeystream;
|
|
8
6
|
private baseCrypt;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* @param {string} text - The plaintext message to be encrypted.
|
|
12
|
-
* @param {string} key - The encryption key.
|
|
13
|
-
* @returns {string} - The encrypted data in Hex format.
|
|
14
|
-
*/
|
|
15
|
-
encrypt: (text: string, key: string, bufferEncoding?: BufferEncoding) => string;
|
|
16
|
-
/**
|
|
17
|
-
* Decrypts an encrypted message using the given key.
|
|
18
|
-
* @param {string} encrypted - The encrypted data in Hex format.
|
|
19
|
-
* @param {string} key - The decryption key.
|
|
20
|
-
* @returns {string} - The decrypted plaintext message.
|
|
21
|
-
*/
|
|
22
|
-
decrypt: (encrypted: string, key: string, bufferEncoding?: BufferEncoding) => string;
|
|
7
|
+
encrypt: (text: string, key: string) => string;
|
|
8
|
+
decrypt: (token: string, key: string) => string | null;
|
|
23
9
|
/**
|
|
24
10
|
* Converts a Hex string to its corresponding ASCII representation.
|
|
25
11
|
* @param {string} hexString - The Hex string to be converted.
|
package/dist/methods/crypt.js
CHANGED
|
@@ -5,41 +5,87 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const crypto_1 = __importDefault(require("crypto"));
|
|
7
7
|
class Crypt {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
8
|
+
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
9
|
+
// Base62 encode: buffer → base62 string
|
|
10
|
+
base62Encode(buffer) {
|
|
11
|
+
let digits = [0];
|
|
12
|
+
for (const byte of buffer) {
|
|
13
|
+
let carry = byte;
|
|
14
|
+
for (let j = 0; j < digits.length; j++) {
|
|
15
|
+
const val = digits[j] * 256 + carry;
|
|
16
|
+
digits[j] = val % 62;
|
|
17
|
+
carry = Math.floor(val / 62);
|
|
18
|
+
}
|
|
19
|
+
while (carry > 0) {
|
|
20
|
+
digits.push(carry % 62);
|
|
21
|
+
carry = Math.floor(carry / 62);
|
|
22
|
+
}
|
|
20
23
|
}
|
|
21
|
-
return
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
return (digits
|
|
25
|
+
.reverse()
|
|
26
|
+
.map((d) => this.chars[d])
|
|
27
|
+
.join("") || "0");
|
|
28
|
+
}
|
|
29
|
+
// Base62 decode: string → buffer
|
|
30
|
+
base62Decode(str) {
|
|
31
|
+
let digits = [0];
|
|
32
|
+
for (const c of str) {
|
|
33
|
+
let carry = this.chars.indexOf(c);
|
|
34
|
+
for (let j = 0; j < digits.length; j++) {
|
|
35
|
+
const val = digits[j] * 62 + carry;
|
|
36
|
+
digits[j] = val & 0xff;
|
|
37
|
+
carry = val >> 8;
|
|
38
|
+
}
|
|
39
|
+
while (carry > 0) {
|
|
40
|
+
digits.push(carry & 0xff);
|
|
41
|
+
carry >>= 8;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return Buffer.from(digits.reverse());
|
|
45
|
+
}
|
|
46
|
+
generateKeystream(key, length) {
|
|
47
|
+
const result = Buffer.alloc(length);
|
|
48
|
+
let pos = 0;
|
|
49
|
+
let counter = 0;
|
|
50
|
+
while (pos < length) {
|
|
51
|
+
const hash = crypto_1.default.createHash("sha256");
|
|
52
|
+
hash.update(key);
|
|
53
|
+
hash.update(Buffer.from(counter.toString()));
|
|
54
|
+
const digest = hash.digest();
|
|
55
|
+
const copy = Math.min(length - pos, digest.length);
|
|
56
|
+
digest.copy(result, pos, 0, copy);
|
|
57
|
+
pos += copy;
|
|
58
|
+
counter++;
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
baseCrypt(input, key) {
|
|
63
|
+
const keystream = this.generateKeystream(key, input.length);
|
|
64
|
+
const output = Buffer.alloc(input.length);
|
|
65
|
+
for (let i = 0; i < input.length; i++) {
|
|
66
|
+
output[i] = input[i] ^ keystream[i];
|
|
67
|
+
}
|
|
68
|
+
return output;
|
|
69
|
+
}
|
|
70
|
+
encrypt = (text, key) => {
|
|
71
|
+
const input = Buffer.from(text, "utf8");
|
|
72
|
+
const encrypted = this.baseCrypt(input, key);
|
|
73
|
+
const cipher = this.base62Encode(encrypted);
|
|
74
|
+
const hmac = crypto_1.default.createHmac("sha256", key).update(cipher).digest();
|
|
75
|
+
const signature = this.base62Encode(hmac);
|
|
76
|
+
return `${cipher}.${signature}`;
|
|
32
77
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
78
|
+
decrypt = (token, key) => {
|
|
79
|
+
const [cipher, signature] = token.split(".");
|
|
80
|
+
if (!cipher || !signature)
|
|
81
|
+
return null;
|
|
82
|
+
const hmac = crypto_1.default.createHmac("sha256", key).update(cipher).digest();
|
|
83
|
+
const expected = this.base62Encode(hmac);
|
|
84
|
+
if (expected !== signature)
|
|
85
|
+
return null;
|
|
86
|
+
const input = this.base62Decode(cipher);
|
|
87
|
+
const decrypted = this.baseCrypt(input, key);
|
|
88
|
+
return decrypted.toString("utf8");
|
|
43
89
|
};
|
|
44
90
|
/**
|
|
45
91
|
* Converts a Hex string to its corresponding ASCII representation.
|