Skip to content

Commit 100e158

Browse files
committed
added tests for other encodings and updated types
1 parent dfb9b50 commit 100e158

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

index.d.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
declare class Cryptr {
2-
constructor(
3-
secret: string,
4-
options?: { pbkdf2Iterations?: number; saltLength?: number },
5-
);
2+
constructor(
3+
secret: string,
4+
options?: { pbkdf2Iterations?: number; saltLength?: number; encoding?: 'hex' | 'base64' | 'latin1' },
5+
);
66

7-
encrypt(value: string): string;
7+
encrypt(value: string): string;
88

9-
decrypt(value: string): string;
9+
decrypt(value: string): string;
1010
}
1111

1212
export = Cryptr;

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Cryptr(secret, options) {
1818

1919
if (options) {
2020
if (options.encoding) {
21-
encoding = options.encoding;
21+
encoding = options.encoding;
2222
}
2323

2424
if (options.pbkdf2Iterations) {

tests/index.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@ test('works...', (t) => {
1515
});
1616

1717
test('works with custom encoding', (t) => {
18-
t.plan(1);
18+
const encodings = ['hex', 'base64', 'latin1'];
1919

20-
const cryptr = new Cryptr(testSecret, { encoding: 'base64' });
21-
const encryptedString = cryptr.encrypt(testData);
22-
const decryptedString = cryptr.decrypt(encryptedString);
20+
t.plan(encodings.length);
2321

24-
t.equal(decryptedString, testData, 'decrypted aes256 correctly with custom encoding');
22+
encodings.forEach((encoding) => {
23+
const cryptr = new Cryptr(testSecret, { encoding });
24+
const encryptedString = cryptr.encrypt(testData);
25+
const decryptedString = cryptr.decrypt(encryptedString);
26+
27+
t.equal(decryptedString, testData, `decrypted correctly with ${encoding} encoding`);
28+
});
2529
});
2630

2731
test('custom encoding affects output length', (t) => {
28-
t.plan(1);
32+
t.plan(1);
2933

30-
const cryptr = new Cryptr(testSecret, { encoding: 'base64' });
31-
const cryptr2 = new Cryptr(testSecret);
32-
const encryptedString = cryptr.encrypt(testData);
33-
const encryptedString2 = cryptr2.encrypt(testData);
34+
const cryptr = new Cryptr(testSecret, { encoding: 'base64' });
35+
const cryptr2 = new Cryptr(testSecret);
36+
const encryptedString = cryptr.encrypt(testData);
37+
const encryptedString2 = cryptr2.encrypt(testData);
3438

35-
t.ok(encryptedString.length < encryptedString2.length, 'custom encoding was shorter');
39+
t.ok(encryptedString.length < encryptedString2.length, 'custom encoding was shorter');
3640
});
3741

3842
test('works with custom pbkdf2Iterations', (t) => {

0 commit comments

Comments
 (0)