-
Notifications
You must be signed in to change notification settings - Fork 2
/
RSA.spec.ts
132 lines (112 loc) · 3.84 KB
/
RSA.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { RSAClient } from "../src/RSAClient";
describe("RSA key initialization: init()", () => {
it("Should generate a key", async () => {
expect.assertions(1);
const RSA = new RSAClient();
await expect(RSA.init()).resolves.not.toThrow();
});
});
describe("RSA key export: exportKey()", () => {
it("Should export key", async () => {
expect.assertions(2);
const RSA = new RSAClient();
await expect(RSA.init()).resolves.not.toThrow();
await expect(RSA.exportPublicKey()).resolves.not.toThrow();
});
it("Should not export key", async () => {
expect.assertions(1);
const RSA = new RSAClient();
await expect(RSA.exportPublicKey()).rejects.toEqual(
"Export failed: public key not found. Call init() first."
);
});
});
describe("RSA key import: importBufferKey()", () => {
it("Should import key", async () => {
expect.assertions(3);
const RSA = new RSAClient();
await expect(RSA.init()).resolves.not.toThrow();
const exportedKey = await RSA.exportPublicKey();
expect(exportedKey).toBeTruthy();
await expect(RSA.importClientPublic(exportedKey)).resolves.not.toThrow();
});
it("Should not import a key", async () => {
const client = new RSAClient();
await client.init();
expect(
client.importClientPublic(new Uint8Array([0x01, 0x00, 0x01]))
).rejects.toThrow();
});
});
describe("RSA encryption", () => {
it("Should encrypt a message", async () => {
const message = "Secret Message";
const client1 = new RSAClient();
await client1.init();
const client1Public = await client1.exportPublicKey();
const client2 = new RSAClient();
await client2.init();
await client2.importClientPublic(client1Public);
expect(client2.encrypt(Buffer.from(message))).resolves.not.toThrow();
expect(client2.encrypt(Buffer.from(message))).resolves.not.toEqual(
Buffer.from(message)
);
});
it("Should not encrypt a message", async () => {
const message = "Secret Message";
const client1 = new RSAClient();
await expect(client1.encrypt(Buffer.from(message))).rejects.toEqual(
"Error while encrypting: No client public key found."
);
});
});
describe("RSA decryption", () => {
it("Should encrypt and decrypt a message", async () => {
const message = "Secret Message";
const client1 = new RSAClient();
await client1.init();
const client1Public = await client1.exportPublicKey();
const client2 = new RSAClient();
await client2.init();
await client2.importClientPublic(client1Public);
const client2Encrypted = await client2.encrypt(Buffer.from(message));
const client1Decrypted = await client1.decrypt(
client2Encrypted as ArrayBuffer
);
expect(client1Decrypted).toBeTruthy();
expect(client1.arrayBufferToString(client1Decrypted)).toBe(message);
});
it("Should encrypt but not decrypt a message", async () => {
const message = "Secret Message";
const client1 = new RSAClient();
await client1.init();
const client3 = new RSAClient();
await client3.init();
const client3Public = await client3.exportPublicKey();
const client2 = new RSAClient();
await client2.init();
await client2.importClientPublic(client3Public);
const client2Encrypted = await client2.encrypt(Buffer.from(message));
await expect(
client1.decrypt(client2Encrypted as ArrayBuffer)
).rejects.toThrow();
});
it("Should not decrypt a message", async () => {
const message = "Secret Message";
const client1 = new RSAClient();
await expect(client1.decrypt(Buffer.from(message))).rejects.toEqual(
"Error while decrypting: No private key found."
);
});
});
describe("PEM export: spkiToPEM()", () => {
it("Should convert to spkiToPEM", async () => {
expect.assertions(3);
const RSA = new RSAClient();
await expect(RSA.init()).resolves.not.toThrow();
const exportedPublic = await RSA.exportPublicKey();
expect(exportedPublic).toBeTruthy();
const pem = RSA.spkiToPEM(exportedPublic);
expect(pem).toBeTruthy();
});
});