@vvlad1973/crypto 2.1.3 → 2.3.0
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/__tests__/crypto.test.js +44 -1
- package/dist/crypto.d.ts +36 -1
- package/dist/crypto.js +51 -1
- package/package.json +2 -2
- package/src/crypto.ts +55 -1
|
@@ -35,7 +35,6 @@ describe('Crypto', () => {
|
|
|
35
35
|
expect(crypto).toBeDefined();
|
|
36
36
|
});
|
|
37
37
|
it('should return UUIDv4', () => {
|
|
38
|
-
const crypto = new Crypto(options);
|
|
39
38
|
const uuid = Crypto.getUUID();
|
|
40
39
|
const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
41
40
|
expect(uuid).toMatch(uuidv4Regex);
|
|
@@ -153,6 +152,50 @@ describe('isCrypto', () => {
|
|
|
153
152
|
expect(isCrypto(plainObject)).toBe(false);
|
|
154
153
|
});
|
|
155
154
|
});
|
|
155
|
+
describe('encryptStorable / decryptStorable', () => {
|
|
156
|
+
const password = 'testPassword';
|
|
157
|
+
const salt = 'testSalt';
|
|
158
|
+
const plainText = 'Hello, World!';
|
|
159
|
+
it('round-trips correctly', () => {
|
|
160
|
+
const crypto = new Crypto(password, salt);
|
|
161
|
+
expect(crypto.decryptStorable(crypto.encryptStorable(plainText))).toBe(plainText);
|
|
162
|
+
});
|
|
163
|
+
it('produces different ciphertext on each call (fresh IV)', () => {
|
|
164
|
+
const crypto = new Crypto(password, salt);
|
|
165
|
+
expect(crypto.encryptStorable(plainText)).not.toBe(crypto.encryptStorable(plainText));
|
|
166
|
+
});
|
|
167
|
+
it('output is longer than encrypt() by exactly 16 bytes (32 hex chars)', () => {
|
|
168
|
+
const crypto = new Crypto(password, salt, 'sha512', 1000, 32, 0);
|
|
169
|
+
const storable = crypto.encryptStorable(plainText);
|
|
170
|
+
const legacy = crypto.encrypt(plainText);
|
|
171
|
+
expect(storable.length).toBe(legacy.length + 32);
|
|
172
|
+
});
|
|
173
|
+
it('two different instances with same key can cross-decrypt', () => {
|
|
174
|
+
const a = new Crypto(password, salt);
|
|
175
|
+
const b = new Crypto(password, salt);
|
|
176
|
+
expect(b.decryptStorable(a.encryptStorable(plainText))).toBe(plainText);
|
|
177
|
+
});
|
|
178
|
+
it('throws RangeError when input is too short', () => {
|
|
179
|
+
const crypto = new Crypto(password, salt);
|
|
180
|
+
expect(() => crypto.decryptStorable('aabbcc')).toThrow(RangeError);
|
|
181
|
+
});
|
|
182
|
+
it('handles empty string', () => {
|
|
183
|
+
const crypto = new Crypto(password, salt);
|
|
184
|
+
expect(crypto.decryptStorable(crypto.encryptStorable(''))).toBe('');
|
|
185
|
+
});
|
|
186
|
+
it('handles Unicode text', () => {
|
|
187
|
+
const crypto = new Crypto(password, salt);
|
|
188
|
+
const unicode = '🔐 Шифрование 中文';
|
|
189
|
+
expect(crypto.decryptStorable(crypto.encryptStorable(unicode))).toBe(unicode);
|
|
190
|
+
});
|
|
191
|
+
it('construction-time IV does not affect result', () => {
|
|
192
|
+
const c0 = new Crypto(password, salt, 'sha512', 1000, 32, 0);
|
|
193
|
+
const c5 = new Crypto(password, salt, 'sha512', 1000, 32, 5);
|
|
194
|
+
// Both should decrypt successfully regardless of instance IV
|
|
195
|
+
expect(c5.decryptStorable(c0.encryptStorable(plainText))).toBe(plainText);
|
|
196
|
+
expect(c0.decryptStorable(c5.encryptStorable(plainText))).toBe(plainText);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
156
199
|
describe('Compatibility with previous version', () => {
|
|
157
200
|
const plainText = 'Проверка связи';
|
|
158
201
|
let options;
|
package/dist/crypto.d.ts
CHANGED
|
@@ -40,16 +40,51 @@ export declare class Crypto {
|
|
|
40
40
|
private convertNumberToIV;
|
|
41
41
|
/**
|
|
42
42
|
* Encrypt a text string.
|
|
43
|
+
* Uses the IV supplied at construction time (fixed or random-at-construct).
|
|
44
|
+
* The IV is **not** embedded in the output — both sides must use the same IV.
|
|
45
|
+
* For persistent storage use {@link encryptStorable} instead.
|
|
46
|
+
*
|
|
43
47
|
* @param {string} text - The plain text to encrypt.
|
|
44
48
|
* @returns {string} The encrypted text as a hex string.
|
|
45
49
|
*/
|
|
46
50
|
encrypt(text: string): string;
|
|
47
51
|
/**
|
|
48
|
-
* Decrypt an encrypted text string.
|
|
52
|
+
* Decrypt an encrypted text string produced by {@link encrypt}.
|
|
53
|
+
* Uses the IV supplied at construction time.
|
|
54
|
+
*
|
|
49
55
|
* @param {string} text - The encrypted text as a hex string.
|
|
50
56
|
* @returns {string} The decrypted plain text.
|
|
51
57
|
*/
|
|
52
58
|
decrypt(text: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Encrypts text and prepends a fresh random IV to the output.
|
|
61
|
+
*
|
|
62
|
+
* Use this method when the ciphertext will be stored (database, file, etc.)
|
|
63
|
+
* and retrieved later in a different process instance. A new random IV is
|
|
64
|
+
* generated for every call, so repeated encryptions of the same plaintext
|
|
65
|
+
* produce different ciphertexts — no IV reuse across records.
|
|
66
|
+
*
|
|
67
|
+
* Output format: `hex( IV[16 bytes] || ciphertext[N bytes] )`
|
|
68
|
+
*
|
|
69
|
+
* The construction-time IV is **not** used by this method.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} text - The plain text to encrypt.
|
|
72
|
+
* @returns {string} Hex string containing the prepended IV and ciphertext.
|
|
73
|
+
* @see {@link decryptStorable}
|
|
74
|
+
*/
|
|
75
|
+
encryptStorable(text: string): string;
|
|
76
|
+
/**
|
|
77
|
+
* Decrypts a value produced by {@link encryptStorable}.
|
|
78
|
+
*
|
|
79
|
+
* Extracts the first 16 bytes as the IV, then decrypts the remainder.
|
|
80
|
+
* The construction-time IV is **not** used by this method.
|
|
81
|
+
*
|
|
82
|
+
* @param {string} text - Hex string `IV[16 bytes] || ciphertext` from {@link encryptStorable}.
|
|
83
|
+
* @returns {string} The decrypted plain text.
|
|
84
|
+
* @throws {RangeError} When the input is shorter than 16 bytes (32 hex chars).
|
|
85
|
+
* @see {@link encryptStorable}
|
|
86
|
+
*/
|
|
87
|
+
decryptStorable(text: string): string;
|
|
53
88
|
/**
|
|
54
89
|
* Returns a UUID ver.4 string.
|
|
55
90
|
* @returns {string} UUID ver.4 string.
|
package/dist/crypto.js
CHANGED
|
@@ -53,6 +53,10 @@ export class Crypto {
|
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* Encrypt a text string.
|
|
56
|
+
* Uses the IV supplied at construction time (fixed or random-at-construct).
|
|
57
|
+
* The IV is **not** embedded in the output — both sides must use the same IV.
|
|
58
|
+
* For persistent storage use {@link encryptStorable} instead.
|
|
59
|
+
*
|
|
56
60
|
* @param {string} text - The plain text to encrypt.
|
|
57
61
|
* @returns {string} The encrypted text as a hex string.
|
|
58
62
|
*/
|
|
@@ -65,7 +69,9 @@ export class Crypto {
|
|
|
65
69
|
return encrypted.toString('hex');
|
|
66
70
|
}
|
|
67
71
|
/**
|
|
68
|
-
* Decrypt an encrypted text string.
|
|
72
|
+
* Decrypt an encrypted text string produced by {@link encrypt}.
|
|
73
|
+
* Uses the IV supplied at construction time.
|
|
74
|
+
*
|
|
69
75
|
* @param {string} text - The encrypted text as a hex string.
|
|
70
76
|
* @returns {string} The decrypted plain text.
|
|
71
77
|
*/
|
|
@@ -78,6 +84,50 @@ export class Crypto {
|
|
|
78
84
|
]);
|
|
79
85
|
return decrypted.toString('utf8');
|
|
80
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Encrypts text and prepends a fresh random IV to the output.
|
|
89
|
+
*
|
|
90
|
+
* Use this method when the ciphertext will be stored (database, file, etc.)
|
|
91
|
+
* and retrieved later in a different process instance. A new random IV is
|
|
92
|
+
* generated for every call, so repeated encryptions of the same plaintext
|
|
93
|
+
* produce different ciphertexts — no IV reuse across records.
|
|
94
|
+
*
|
|
95
|
+
* Output format: `hex( IV[16 bytes] || ciphertext[N bytes] )`
|
|
96
|
+
*
|
|
97
|
+
* The construction-time IV is **not** used by this method.
|
|
98
|
+
*
|
|
99
|
+
* @param {string} text - The plain text to encrypt.
|
|
100
|
+
* @returns {string} Hex string containing the prepended IV and ciphertext.
|
|
101
|
+
* @see {@link decryptStorable}
|
|
102
|
+
*/
|
|
103
|
+
encryptStorable(text) {
|
|
104
|
+
const iv = randomBytes(16);
|
|
105
|
+
const cipher = createCipheriv('aes-256-ctr', this.key, iv);
|
|
106
|
+
const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]);
|
|
107
|
+
return Buffer.concat([iv, encrypted]).toString('hex');
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Decrypts a value produced by {@link encryptStorable}.
|
|
111
|
+
*
|
|
112
|
+
* Extracts the first 16 bytes as the IV, then decrypts the remainder.
|
|
113
|
+
* The construction-time IV is **not** used by this method.
|
|
114
|
+
*
|
|
115
|
+
* @param {string} text - Hex string `IV[16 bytes] || ciphertext` from {@link encryptStorable}.
|
|
116
|
+
* @returns {string} The decrypted plain text.
|
|
117
|
+
* @throws {RangeError} When the input is shorter than 16 bytes (32 hex chars).
|
|
118
|
+
* @see {@link encryptStorable}
|
|
119
|
+
*/
|
|
120
|
+
decryptStorable(text) {
|
|
121
|
+
const buf = Buffer.from(text, 'hex');
|
|
122
|
+
if (buf.length < 16) {
|
|
123
|
+
throw new RangeError(`decryptStorable: input too short (${buf.length} bytes); ` +
|
|
124
|
+
'expected at least 16 bytes for the IV prefix.');
|
|
125
|
+
}
|
|
126
|
+
const iv = buf.subarray(0, 16);
|
|
127
|
+
const ciphertext = buf.subarray(16);
|
|
128
|
+
const decipher = createDecipheriv('aes-256-ctr', this.key, iv);
|
|
129
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8');
|
|
130
|
+
}
|
|
81
131
|
/**
|
|
82
132
|
* Returns a UUID ver.4 string.
|
|
83
133
|
* @returns {string} UUID ver.4 string.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvlad1973/crypto",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"license": "MIT with Commercial Use",
|
|
18
18
|
"description": "A JavaScript class for encrypting and decrypting text using specified cryptographic parameters (algorithm, password, etc.)",
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@types/node": "^25.
|
|
20
|
+
"@types/node": "^25.3.3",
|
|
21
21
|
"@typescript-eslint/parser": "^7.18.0",
|
|
22
22
|
"@vitest/coverage-v8": "^3.2.4",
|
|
23
23
|
"@vitest/ui": "^3.2.4",
|
package/src/crypto.ts
CHANGED
|
@@ -102,6 +102,10 @@ export class Crypto {
|
|
|
102
102
|
|
|
103
103
|
/**
|
|
104
104
|
* Encrypt a text string.
|
|
105
|
+
* Uses the IV supplied at construction time (fixed or random-at-construct).
|
|
106
|
+
* The IV is **not** embedded in the output — both sides must use the same IV.
|
|
107
|
+
* For persistent storage use {@link encryptStorable} instead.
|
|
108
|
+
*
|
|
105
109
|
* @param {string} text - The plain text to encrypt.
|
|
106
110
|
* @returns {string} The encrypted text as a hex string.
|
|
107
111
|
*/
|
|
@@ -115,7 +119,9 @@ export class Crypto {
|
|
|
115
119
|
}
|
|
116
120
|
|
|
117
121
|
/**
|
|
118
|
-
* Decrypt an encrypted text string.
|
|
122
|
+
* Decrypt an encrypted text string produced by {@link encrypt}.
|
|
123
|
+
* Uses the IV supplied at construction time.
|
|
124
|
+
*
|
|
119
125
|
* @param {string} text - The encrypted text as a hex string.
|
|
120
126
|
* @returns {string} The decrypted plain text.
|
|
121
127
|
*/
|
|
@@ -129,6 +135,54 @@ export class Crypto {
|
|
|
129
135
|
return decrypted.toString('utf8');
|
|
130
136
|
}
|
|
131
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Encrypts text and prepends a fresh random IV to the output.
|
|
140
|
+
*
|
|
141
|
+
* Use this method when the ciphertext will be stored (database, file, etc.)
|
|
142
|
+
* and retrieved later in a different process instance. A new random IV is
|
|
143
|
+
* generated for every call, so repeated encryptions of the same plaintext
|
|
144
|
+
* produce different ciphertexts — no IV reuse across records.
|
|
145
|
+
*
|
|
146
|
+
* Output format: `hex( IV[16 bytes] || ciphertext[N bytes] )`
|
|
147
|
+
*
|
|
148
|
+
* The construction-time IV is **not** used by this method.
|
|
149
|
+
*
|
|
150
|
+
* @param {string} text - The plain text to encrypt.
|
|
151
|
+
* @returns {string} Hex string containing the prepended IV and ciphertext.
|
|
152
|
+
* @see {@link decryptStorable}
|
|
153
|
+
*/
|
|
154
|
+
public encryptStorable(text: string): string {
|
|
155
|
+
const iv = randomBytes(16);
|
|
156
|
+
const cipher = createCipheriv('aes-256-ctr', this.key, iv);
|
|
157
|
+
const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]);
|
|
158
|
+
return Buffer.concat([iv, encrypted]).toString('hex');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Decrypts a value produced by {@link encryptStorable}.
|
|
163
|
+
*
|
|
164
|
+
* Extracts the first 16 bytes as the IV, then decrypts the remainder.
|
|
165
|
+
* The construction-time IV is **not** used by this method.
|
|
166
|
+
*
|
|
167
|
+
* @param {string} text - Hex string `IV[16 bytes] || ciphertext` from {@link encryptStorable}.
|
|
168
|
+
* @returns {string} The decrypted plain text.
|
|
169
|
+
* @throws {RangeError} When the input is shorter than 16 bytes (32 hex chars).
|
|
170
|
+
* @see {@link encryptStorable}
|
|
171
|
+
*/
|
|
172
|
+
public decryptStorable(text: string): string {
|
|
173
|
+
const buf = Buffer.from(text, 'hex');
|
|
174
|
+
if (buf.length < 16) {
|
|
175
|
+
throw new RangeError(
|
|
176
|
+
`decryptStorable: input too short (${buf.length} bytes); ` +
|
|
177
|
+
'expected at least 16 bytes for the IV prefix.',
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const iv = buf.subarray(0, 16);
|
|
181
|
+
const ciphertext = buf.subarray(16);
|
|
182
|
+
const decipher = createDecipheriv('aes-256-ctr', this.key, iv);
|
|
183
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8');
|
|
184
|
+
}
|
|
185
|
+
|
|
132
186
|
/**
|
|
133
187
|
* Returns a UUID ver.4 string.
|
|
134
188
|
* @returns {string} UUID ver.4 string.
|