Skip to content

Commit ca881d4

Browse files
author
Simon Hofmann
committed
(#11) Updated decrypt method to work with different algorithms
1 parent 60c14da commit ca881d4

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

lib/decrypt.function.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {Algorithm} from "./algorithm.enum";
12
import {decrypt} from "./decrypt.function";
23

34
describe("secret", () => {
@@ -8,7 +9,7 @@ describe("secret", () => {
89
const expected = "Can you keep a secret?";
910

1011
// THEN
11-
expect(decrypt(input, key)).resolves.toBe(expected);
12+
expect(decrypt(input, key, Algorithm.AES128CBC)).resolves.toBe(expected);
1213
});
1314

1415
it("should throw an error when IV size is invalid", async () => {
@@ -17,6 +18,7 @@ describe("secret", () => {
1718
const input = "Zm9vCg==";
1819

1920
// THEN
20-
expect(decrypt(input, key)).rejects.toThrowError("Invalid IV size. Size of 16 byte required.");
21+
expect(decrypt(input, key, Algorithm.AES128CBC))
22+
.rejects.toThrowError("Invalid IV size. Size of 16 byte required.");
2123
});
2224
});

lib/decrypt.function.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
import * as crypto from "crypto";
2+
import {Algorithm} from "./algorithm.enum";
23
import {base64EncodedBufferFromString} from "./buffer.function";
3-
import {getAlgorithm} from "./get-algorithm.function";
4+
import {CipherSpec, getCipherSpec} from "./get-cipher-spec.function";
45

5-
export const decrypt = async (encrypted: string, secret: string) => {
6-
const cipherSpec = getAlgorithm(secret);
6+
interface DecryptInput {
7+
iv: Buffer;
8+
content: Buffer;
9+
}
10+
11+
const sliceInput = (plainInput: Buffer, cipherSpec: CipherSpec): DecryptInput => {
12+
const iv = plainInput.slice(0, cipherSpec.blockSize);
13+
const content = plainInput.slice(cipherSpec.blockSize);
14+
15+
return ({
16+
content,
17+
iv
18+
});
19+
};
20+
21+
export const decrypt = async (encrypted: string, secret: string, algorithm: Algorithm, useIV: boolean = true) => {
22+
const cipherSpec = getCipherSpec(algorithm);
723
const key = base64EncodedBufferFromString(secret);
824
const plainInput = base64EncodedBufferFromString(encrypted);
925

10-
if (plainInput.length < cipherSpec.blockSize) {
26+
if (useIV && plainInput.length < cipherSpec.blockSize) {
1127
throw new Error(`Invalid IV size. Size of ${cipherSpec.blockSize} byte required.`);
1228
}
1329

14-
const iv = plainInput.slice(0, cipherSpec.blockSize);
15-
const content = plainInput.slice(cipherSpec.blockSize);
30+
let decipher: crypto.Decipher;
31+
let data: DecryptInput;
32+
if (useIV) {
33+
data = sliceInput(plainInput, cipherSpec);
1634

17-
const decipher = crypto.createDecipheriv(
18-
cipherSpec.cipher,
19-
key,
20-
iv
21-
);
22-
const part = decipher.update(content).toString("utf8");
35+
decipher = crypto.createDecipheriv(
36+
algorithm,
37+
key,
38+
data.iv
39+
);
40+
} else {
41+
data = {iv: Buffer.alloc(0), content: plainInput};
42+
decipher = crypto.createDecipher(
43+
algorithm,
44+
key
45+
);
46+
}
47+
const part = decipher.update(data.content).toString("utf8");
2348
return part + decipher.final("utf8");
2449
};

0 commit comments

Comments
 (0)