@nivinjoseph/n-sec 5.0.4 → 6.0.1
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/.eslintrc +13 -4
- package/.vscode/settings.json +103 -0
- package/.yarn/releases/yarn-4.0.2.cjs +893 -0
- package/.yarnrc.yml +3 -0
- package/dist/api-security/alg-type.d.ts +1 -0
- package/dist/api-security/alg-type.d.ts.map +1 -0
- package/dist/api-security/alg-type.js +2 -5
- package/dist/api-security/alg-type.js.map +1 -1
- package/dist/api-security/claim.d.ts +1 -0
- package/dist/api-security/claim.d.ts.map +1 -0
- package/dist/api-security/claim.js +5 -9
- package/dist/api-security/claim.js.map +1 -1
- package/dist/api-security/claims-identity.d.ts +2 -1
- package/dist/api-security/claims-identity.d.ts.map +1 -0
- package/dist/api-security/claims-identity.js +4 -8
- package/dist/api-security/claims-identity.js.map +1 -1
- package/dist/api-security/expired-token-exception.d.ts +1 -0
- package/dist/api-security/expired-token-exception.d.ts.map +1 -0
- package/dist/api-security/expired-token-exception.js +5 -9
- package/dist/api-security/expired-token-exception.js.map +1 -1
- package/dist/api-security/invalid-token-exception.d.ts +1 -0
- package/dist/api-security/invalid-token-exception.d.ts.map +1 -0
- package/dist/api-security/invalid-token-exception.js +7 -11
- package/dist/api-security/invalid-token-exception.js.map +1 -1
- package/dist/api-security/json-web-token.d.ts +3 -2
- package/dist/api-security/json-web-token.d.ts.map +1 -0
- package/dist/api-security/json-web-token.js +39 -43
- package/dist/api-security/json-web-token.js.map +1 -1
- package/dist/api-security/security-token.d.ts +1 -0
- package/dist/api-security/security-token.d.ts.map +1 -0
- package/dist/api-security/security-token.js +6 -10
- package/dist/api-security/security-token.js.map +1 -1
- package/dist/bin.d.ts +1 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +15 -20
- package/dist/bin.js.map +1 -1
- package/dist/crypto/asymmetric-encryption.d.ts +2 -0
- package/dist/crypto/asymmetric-encryption.d.ts.map +1 -0
- package/dist/crypto/asymmetric-encryption.js +1 -1
- package/dist/crypto/asymmetric-encryption.js.map +1 -1
- package/dist/crypto/crypto-exception.d.ts +1 -0
- package/dist/crypto/crypto-exception.d.ts.map +1 -0
- package/dist/crypto/crypto-exception.js +2 -6
- package/dist/crypto/crypto-exception.js.map +1 -1
- package/dist/crypto/digital-signature.d.ts +2 -0
- package/dist/crypto/digital-signature.d.ts.map +1 -0
- package/dist/crypto/digital-signature.js +1 -1
- package/dist/crypto/digital-signature.js.map +1 -1
- package/dist/crypto/hash.d.ts +1 -0
- package/dist/crypto/hash.d.ts.map +1 -0
- package/dist/crypto/hash.js +9 -12
- package/dist/crypto/hash.js.map +1 -1
- package/dist/crypto/hmac.d.ts +1 -0
- package/dist/crypto/hmac.d.ts.map +1 -0
- package/dist/crypto/hmac.js +6 -10
- package/dist/crypto/hmac.js.map +1 -1
- package/dist/crypto/symmetric-encryption.d.ts +1 -0
- package/dist/crypto/symmetric-encryption.d.ts.map +1 -0
- package/dist/crypto/symmetric-encryption.js +13 -17
- package/dist/crypto/symmetric-encryption.js.map +1 -1
- package/dist/index.d.ts +14 -12
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -27
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.json +2 -1
- package/package.json +23 -24
- package/src/api-security/claims-identity.ts +1 -1
- package/src/api-security/json-web-token.ts +21 -21
- package/src/bin.ts +1 -1
- package/src/crypto/hash.ts +11 -9
- package/src/crypto/hmac.ts +7 -7
- package/src/crypto/symmetric-encryption.ts +11 -11
- package/src/index.ts +19 -18
- package/test/asymmetric-encryption.test.ts +37 -37
- package/test/digital-signature.test.ts +22 -22
- package/test/hash.test.ts +120 -105
- package/test/hmac.test.ts +56 -55
- package/test/json-web-token.test.ts +95 -95
- package/test/other.test.ts +8 -7
- package/test/symmetric-encryption.test.ts +49 -48
- package/tsconfig.json +8 -5
package/test/hash.test.ts
CHANGED
|
@@ -1,49 +1,50 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { describe, test } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { Hash } from "./../src/index.js";
|
|
3
4
|
import "@nivinjoseph/n-ext";
|
|
4
5
|
// import { CryptoException } from "./../src/crypto-exception";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
await describe("Hash", async () =>
|
|
7
8
|
{
|
|
8
|
-
|
|
9
|
+
await describe("create", async () =>
|
|
9
10
|
{
|
|
10
|
-
test("must return a string value that is not null, empty, whitespace or same as input when called with a valid input", () =>
|
|
11
|
+
await test("must return a string value that is not null, empty, whitespace or same as input when called with a valid input", () =>
|
|
11
12
|
{
|
|
12
13
|
const input = "hello world";
|
|
13
14
|
const hash = Hash.create(input);
|
|
14
15
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
assert.ok(hash !== null && !hash.isEmptyOrWhiteSpace());
|
|
17
|
+
assert.notStrictEqual(hash, input);
|
|
17
18
|
});
|
|
18
|
-
|
|
19
|
-
test("multiple invocations with the same input must return the same output", () =>
|
|
19
|
+
|
|
20
|
+
await test("multiple invocations with the same input must return the same output", () =>
|
|
20
21
|
{
|
|
21
22
|
const input = "hello world";
|
|
22
23
|
const hash1 = Hash.create(input);
|
|
23
24
|
const hash2 = Hash.create(input);
|
|
24
|
-
|
|
25
|
+
assert.strictEqual(hash1, hash2);
|
|
25
26
|
});
|
|
26
|
-
|
|
27
|
-
test("multiple invocations with the different inputs must return different outputs", () =>
|
|
27
|
+
|
|
28
|
+
await test("multiple invocations with the different inputs must return different outputs", () =>
|
|
28
29
|
{
|
|
29
30
|
const input1 = "hello world";
|
|
30
31
|
const hash1 = Hash.create(input1);
|
|
31
32
|
const input2 = "goodbye world";
|
|
32
33
|
const hash2 = Hash.create(input2);
|
|
33
|
-
|
|
34
|
+
assert.notStrictEqual(hash1, hash2);
|
|
34
35
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// test("successfully create a hash twice with different values", async () =>
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// await test("successfully create a hash twice with different values", async () =>
|
|
38
39
|
// {
|
|
39
40
|
// let hash1 = await Hash.create("hello world");
|
|
40
41
|
// let hash2 = await Hash.create("hello world2");
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
42
|
+
// assert.ok(hash1 !== null && !hash1.isEmptyOrWhiteSpace());
|
|
43
|
+
// assert.ok(hash2 !== null && !hash2.isEmptyOrWhiteSpace());
|
|
44
|
+
// assert.notStrictEqual(hash1, hash2);
|
|
44
45
|
// });
|
|
45
|
-
|
|
46
|
-
// test("should throw CryptoException when value is null", async () =>
|
|
46
|
+
|
|
47
|
+
// await test("should throw CryptoException when value is null", async () =>
|
|
47
48
|
// {
|
|
48
49
|
// try
|
|
49
50
|
// {
|
|
@@ -51,14 +52,14 @@ suite("Hash", () =>
|
|
|
51
52
|
// }
|
|
52
53
|
// catch (exception)
|
|
53
54
|
// {
|
|
54
|
-
//
|
|
55
|
-
//
|
|
55
|
+
// assert.ok(exception instanceof CryptoException);
|
|
56
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
56
57
|
// return;
|
|
57
58
|
// }
|
|
58
|
-
//
|
|
59
|
+
// assert.ok(false);
|
|
59
60
|
// });
|
|
60
|
-
|
|
61
|
-
// test("should throw CryptoException when value is undefined", async () =>
|
|
61
|
+
|
|
62
|
+
// await test("should throw CryptoException when value is undefined", async () =>
|
|
62
63
|
// {
|
|
63
64
|
// try
|
|
64
65
|
// {
|
|
@@ -66,14 +67,14 @@ suite("Hash", () =>
|
|
|
66
67
|
// }
|
|
67
68
|
// catch (exception)
|
|
68
69
|
// {
|
|
69
|
-
//
|
|
70
|
-
//
|
|
70
|
+
// assert.ok(exception instanceof CryptoException);
|
|
71
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
71
72
|
// return;
|
|
72
73
|
// }
|
|
73
|
-
//
|
|
74
|
+
// assert.ok(false);
|
|
74
75
|
// });
|
|
75
|
-
|
|
76
|
-
// test("should throw CryptoException when value is empty string", async () =>
|
|
76
|
+
|
|
77
|
+
// await test("should throw CryptoException when value is empty string", async () =>
|
|
77
78
|
// {
|
|
78
79
|
// try
|
|
79
80
|
// {
|
|
@@ -81,51 +82,51 @@ suite("Hash", () =>
|
|
|
81
82
|
// }
|
|
82
83
|
// catch (exception)
|
|
83
84
|
// {
|
|
84
|
-
//
|
|
85
|
-
//
|
|
85
|
+
// assert.ok(exception instanceof CryptoException);
|
|
86
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
86
87
|
// return;
|
|
87
88
|
// }
|
|
88
|
-
//
|
|
89
|
+
// assert.ok(false);
|
|
89
90
|
// });
|
|
90
|
-
|
|
91
|
+
|
|
91
92
|
});
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
|
|
94
|
+
await describe("createUsingSalt", async () =>
|
|
94
95
|
{
|
|
95
|
-
test("must return a string value that is not null, empty, whitespace or same as input or salt when called with a valid input and salt", () =>
|
|
96
|
+
await test("must return a string value that is not null, empty, whitespace or same as input or salt when called with a valid input and salt", () =>
|
|
96
97
|
{
|
|
97
98
|
const input = "hello world";
|
|
98
99
|
const salt = "salt";
|
|
99
100
|
const hash = Hash.createUsingSalt(input, salt);
|
|
100
101
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
assert.ok(hash !== null && !hash.isEmptyOrWhiteSpace());
|
|
103
|
+
assert.notStrictEqual(hash, input);
|
|
104
|
+
assert.notStrictEqual(hash, salt);
|
|
104
105
|
});
|
|
105
106
|
|
|
106
|
-
test("multiple invocations with the same input and salt must return the same output", () =>
|
|
107
|
+
await test("multiple invocations with the same input and salt must return the same output", () =>
|
|
107
108
|
{
|
|
108
109
|
const input = "hello world";
|
|
109
110
|
const salt = "salt";
|
|
110
111
|
const hash1 = Hash.createUsingSalt(input, salt);
|
|
111
112
|
const hash2 = Hash.createUsingSalt(input, salt);
|
|
112
|
-
|
|
113
|
+
assert.strictEqual(hash1, hash2);
|
|
113
114
|
});
|
|
114
115
|
|
|
115
|
-
test("multiple invocations with different inputs and different salts must return different outputs", () =>
|
|
116
|
+
await test("multiple invocations with different inputs and different salts must return different outputs", () =>
|
|
116
117
|
{
|
|
117
118
|
const input1 = "hello world";
|
|
118
119
|
const salt1 = "salt-1";
|
|
119
120
|
const hash1 = Hash.createUsingSalt(input1, salt1);
|
|
120
|
-
|
|
121
|
+
|
|
121
122
|
const input2 = "goodbye world";
|
|
122
123
|
const salt2 = "salt-2";
|
|
123
124
|
const hash2 = Hash.createUsingSalt(input2, salt2);
|
|
124
|
-
|
|
125
|
-
|
|
125
|
+
|
|
126
|
+
assert.notStrictEqual(hash1, hash2);
|
|
126
127
|
});
|
|
127
|
-
|
|
128
|
-
test("multiple invocations with different inputs and the same salt must return different outputs", () =>
|
|
128
|
+
|
|
129
|
+
await test("multiple invocations with different inputs and the same salt must return different outputs", () =>
|
|
129
130
|
{
|
|
130
131
|
const input1 = "hello world";
|
|
131
132
|
const salt1 = "salt-1";
|
|
@@ -134,10 +135,10 @@ suite("Hash", () =>
|
|
|
134
135
|
const input2 = "goodbye world";
|
|
135
136
|
const hash2 = Hash.createUsingSalt(input2, salt1);
|
|
136
137
|
|
|
137
|
-
|
|
138
|
+
assert.notStrictEqual(hash1, hash2);
|
|
138
139
|
});
|
|
139
|
-
|
|
140
|
-
test("multiple invocations with the same input and different salts must return different outputs", () =>
|
|
140
|
+
|
|
141
|
+
await test("multiple invocations with the same input and different salts must return different outputs", () =>
|
|
141
142
|
{
|
|
142
143
|
const input = "hello world";
|
|
143
144
|
const salt1 = "salt-1";
|
|
@@ -146,48 +147,62 @@ suite("Hash", () =>
|
|
|
146
147
|
const salt2 = "salt-2";
|
|
147
148
|
const hash2 = Hash.createUsingSalt(input, salt2);
|
|
148
149
|
|
|
149
|
-
|
|
150
|
+
assert.notStrictEqual(hash1, hash2);
|
|
150
151
|
});
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
152
|
+
|
|
153
|
+
await test("regex issue", () =>
|
|
154
|
+
{
|
|
155
|
+
let password = "YQAt3TPI7s1YXyClbQ2$&JdHOKZJ@z4!";
|
|
156
|
+
password = password.trim();
|
|
157
|
+
|
|
158
|
+
const createdAt = Date.now();
|
|
159
|
+
const username = "defaultUser";
|
|
160
|
+
|
|
161
|
+
const salt = (createdAt % 2) === 0
|
|
162
|
+
? `${username.base64Decode()}${createdAt}`
|
|
163
|
+
: `${createdAt}${username.base64Decode()}`;
|
|
164
|
+
|
|
165
|
+
Hash.createUsingSalt(password, salt);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
// await test("successfully create a hash", async () =>
|
|
157
172
|
// {
|
|
158
173
|
// let hash = await Hash.createUsingSalt("hello world", "some-salt");
|
|
159
|
-
//
|
|
160
|
-
//
|
|
174
|
+
// assert.ok(hash !== null && !hash.isEmptyOrWhiteSpace());
|
|
175
|
+
// assert.notStrictEqual("hello world", hash);
|
|
161
176
|
// });
|
|
162
177
|
|
|
163
|
-
// test("successfully create a hash twice with same values and same salt", async () =>
|
|
178
|
+
// await test("successfully create a hash twice with same values and same salt", async () =>
|
|
164
179
|
// {
|
|
165
180
|
// let hash1 = await Hash.createUsingSalt("hello world", "some-salt");
|
|
166
181
|
// let hash2 = await Hash.createUsingSalt("hello world", "some-salt");
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
//
|
|
182
|
+
// assert.ok(hash1 !== null && !hash1.isEmptyOrWhiteSpace());
|
|
183
|
+
// assert.ok(hash2 !== null && !hash2.isEmptyOrWhiteSpace());
|
|
184
|
+
// assert.strictEqual(hash1, hash2);
|
|
170
185
|
// });
|
|
171
|
-
|
|
172
|
-
// test("successfully create a hash twice with same values and different salt", async () =>
|
|
186
|
+
|
|
187
|
+
// await test("successfully create a hash twice with same values and different salt", async () =>
|
|
173
188
|
// {
|
|
174
189
|
// let hash1 = await Hash.createUsingSalt("hello world", "some-salt");
|
|
175
190
|
// let hash2 = await Hash.createUsingSalt("hello world", "some-other-salt");
|
|
176
|
-
//
|
|
177
|
-
//
|
|
178
|
-
//
|
|
191
|
+
// assert.ok(hash1 !== null && !hash1.isEmptyOrWhiteSpace());
|
|
192
|
+
// assert.ok(hash2 !== null && !hash2.isEmptyOrWhiteSpace());
|
|
193
|
+
// assert.notStrictEqual(hash1, hash2);
|
|
179
194
|
// });
|
|
180
|
-
|
|
181
|
-
// test("successfully create a hash twice with different values same salt", async () =>
|
|
195
|
+
|
|
196
|
+
// await test("successfully create a hash twice with different values same salt", async () =>
|
|
182
197
|
// {
|
|
183
198
|
// let hash1 = await Hash.createUsingSalt("hello world", "some-salt");
|
|
184
199
|
// let hash2 = await Hash.createUsingSalt("hello world2", "some-salt");
|
|
185
|
-
//
|
|
186
|
-
//
|
|
187
|
-
//
|
|
200
|
+
// assert.ok(hash1 !== null && !hash1.isEmptyOrWhiteSpace());
|
|
201
|
+
// assert.ok(hash2 !== null && !hash2.isEmptyOrWhiteSpace());
|
|
202
|
+
// assert.notStrictEqual(hash1, hash2);
|
|
188
203
|
// });
|
|
189
|
-
|
|
190
|
-
// test("should throw CryptoException when value is null", async () =>
|
|
204
|
+
|
|
205
|
+
// await test("should throw CryptoException when value is null", async () =>
|
|
191
206
|
// {
|
|
192
207
|
// try
|
|
193
208
|
// {
|
|
@@ -195,14 +210,14 @@ suite("Hash", () =>
|
|
|
195
210
|
// }
|
|
196
211
|
// catch (exception)
|
|
197
212
|
// {
|
|
198
|
-
//
|
|
199
|
-
//
|
|
213
|
+
// assert.ok(exception instanceof CryptoException);
|
|
214
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
200
215
|
// return;
|
|
201
216
|
// }
|
|
202
|
-
//
|
|
217
|
+
// assert.ok(false);
|
|
203
218
|
// });
|
|
204
|
-
|
|
205
|
-
// test("should throw CryptoException when value is undefined", async () =>
|
|
219
|
+
|
|
220
|
+
// await test("should throw CryptoException when value is undefined", async () =>
|
|
206
221
|
// {
|
|
207
222
|
// try
|
|
208
223
|
// {
|
|
@@ -210,14 +225,14 @@ suite("Hash", () =>
|
|
|
210
225
|
// }
|
|
211
226
|
// catch (exception)
|
|
212
227
|
// {
|
|
213
|
-
//
|
|
214
|
-
//
|
|
228
|
+
// assert.ok(exception instanceof CryptoException);
|
|
229
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
215
230
|
// return;
|
|
216
231
|
// }
|
|
217
|
-
//
|
|
232
|
+
// assert.ok(false);
|
|
218
233
|
// });
|
|
219
|
-
|
|
220
|
-
// test("should throw CryptoException when salt is null", async () =>
|
|
234
|
+
|
|
235
|
+
// await test("should throw CryptoException when salt is null", async () =>
|
|
221
236
|
// {
|
|
222
237
|
// try
|
|
223
238
|
// {
|
|
@@ -225,14 +240,14 @@ suite("Hash", () =>
|
|
|
225
240
|
// }
|
|
226
241
|
// catch (exception)
|
|
227
242
|
// {
|
|
228
|
-
//
|
|
229
|
-
//
|
|
243
|
+
// assert.ok(exception instanceof CryptoException);
|
|
244
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
230
245
|
// return;
|
|
231
246
|
// }
|
|
232
|
-
//
|
|
247
|
+
// assert.ok(false);
|
|
233
248
|
// });
|
|
234
|
-
|
|
235
|
-
// test("should throw CryptoException when salt is undefined", async () =>
|
|
249
|
+
|
|
250
|
+
// await test("should throw CryptoException when salt is undefined", async () =>
|
|
236
251
|
// {
|
|
237
252
|
// try
|
|
238
253
|
// {
|
|
@@ -240,14 +255,14 @@ suite("Hash", () =>
|
|
|
240
255
|
// }
|
|
241
256
|
// catch (exception)
|
|
242
257
|
// {
|
|
243
|
-
//
|
|
244
|
-
//
|
|
258
|
+
// assert.ok(exception instanceof CryptoException);
|
|
259
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
245
260
|
// return;
|
|
246
261
|
// }
|
|
247
|
-
//
|
|
262
|
+
// assert.ok(false);
|
|
248
263
|
// });
|
|
249
|
-
|
|
250
|
-
// test("should throw CryptoException when salt is empty string", async () =>
|
|
264
|
+
|
|
265
|
+
// await test("should throw CryptoException when salt is empty string", async () =>
|
|
251
266
|
// {
|
|
252
267
|
// try
|
|
253
268
|
// {
|
|
@@ -255,14 +270,14 @@ suite("Hash", () =>
|
|
|
255
270
|
// }
|
|
256
271
|
// catch (exception)
|
|
257
272
|
// {
|
|
258
|
-
//
|
|
259
|
-
//
|
|
273
|
+
// assert.ok(exception instanceof CryptoException);
|
|
274
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
260
275
|
// return;
|
|
261
276
|
// }
|
|
262
|
-
//
|
|
277
|
+
// assert.ok(false);
|
|
263
278
|
// });
|
|
264
|
-
|
|
265
|
-
// test("should throw CryptoException when value is empty string", async () =>
|
|
279
|
+
|
|
280
|
+
// await test("should throw CryptoException when value is empty string", async () =>
|
|
266
281
|
// {
|
|
267
282
|
// try
|
|
268
283
|
// {
|
|
@@ -270,11 +285,11 @@ suite("Hash", () =>
|
|
|
270
285
|
// }
|
|
271
286
|
// catch (exception)
|
|
272
287
|
// {
|
|
273
|
-
//
|
|
274
|
-
//
|
|
288
|
+
// assert.ok(exception instanceof CryptoException);
|
|
289
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
275
290
|
// return;
|
|
276
291
|
// }
|
|
277
|
-
//
|
|
292
|
+
// assert.ok(false);
|
|
278
293
|
// });
|
|
279
294
|
});
|
|
280
295
|
});
|
package/test/hmac.test.ts
CHANGED
|
@@ -1,79 +1,80 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { describe, test } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { Hmac, SymmetricEncryption } from "./../src/index.js";
|
|
3
4
|
// import { CryptoException } from "./../src/crypto-exception";
|
|
4
5
|
import "@nivinjoseph/n-ext";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
await describe("Hmac", async () =>
|
|
7
8
|
{
|
|
8
|
-
|
|
9
|
+
await describe("create", async () =>
|
|
9
10
|
{
|
|
10
|
-
test("should return string value that is not null, empty, whitespace or same as the key or input", async () =>
|
|
11
|
+
await test("should return string value that is not null, empty, whitespace or same as the key or input", async () =>
|
|
11
12
|
{
|
|
12
13
|
const key = await SymmetricEncryption.generateKey();
|
|
13
14
|
const value = "hello world";
|
|
14
15
|
const hmac = Hmac.create(key, value);
|
|
15
16
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
assert.ok(hmac !== null && !hmac.isEmptyOrWhiteSpace());
|
|
18
|
+
assert.notStrictEqual(hmac, key);
|
|
19
|
+
assert.notStrictEqual(hmac, value);
|
|
19
20
|
});
|
|
20
|
-
|
|
21
|
-
test("multiple invocations with the same key and value must return the same output", async () =>
|
|
21
|
+
|
|
22
|
+
await test("multiple invocations with the same key and value must return the same output", async () =>
|
|
22
23
|
{
|
|
23
24
|
const key = await SymmetricEncryption.generateKey();
|
|
24
25
|
const value = "hello world";
|
|
25
26
|
const hmac1 = Hmac.create(key, value);
|
|
26
27
|
const hmac2 = Hmac.create(key, value);
|
|
27
|
-
|
|
28
|
+
assert.strictEqual(hmac1, hmac2);
|
|
28
29
|
});
|
|
29
|
-
|
|
30
|
-
test("multiple invocations with different keys and different values must return different outputs", async () =>
|
|
30
|
+
|
|
31
|
+
await test("multiple invocations with different keys and different values must return different outputs", async () =>
|
|
31
32
|
{
|
|
32
33
|
const key1 = await SymmetricEncryption.generateKey();
|
|
33
34
|
const value1 = "hello world";
|
|
34
35
|
const hmac1 = Hmac.create(key1, value1);
|
|
35
|
-
|
|
36
|
+
|
|
36
37
|
const key2 = await SymmetricEncryption.generateKey();
|
|
37
38
|
const value2 = "goodbye world";
|
|
38
39
|
const hmac2 = Hmac.create(key2, value2);
|
|
39
|
-
|
|
40
|
+
assert.notStrictEqual(hmac1, hmac2);
|
|
40
41
|
});
|
|
41
|
-
|
|
42
|
-
test("multiple invocations with the same key and different values must return different outputs", async () =>
|
|
42
|
+
|
|
43
|
+
await test("multiple invocations with the same key and different values must return different outputs", async () =>
|
|
43
44
|
{
|
|
44
45
|
const key = await SymmetricEncryption.generateKey();
|
|
45
46
|
const value1 = "hello world";
|
|
46
47
|
const value2 = "goodbye world";
|
|
47
48
|
const hmac1 = Hmac.create(key, value1);
|
|
48
49
|
const hmac2 = Hmac.create(key, value2);
|
|
49
|
-
|
|
50
|
+
assert.notStrictEqual(hmac1, hmac2);
|
|
50
51
|
});
|
|
51
|
-
|
|
52
|
-
test("multiple invocations with different keys and the same value must return different outputs", async () =>
|
|
52
|
+
|
|
53
|
+
await test("multiple invocations with different keys and the same value must return different outputs", async () =>
|
|
53
54
|
{
|
|
54
55
|
const key1 = await SymmetricEncryption.generateKey();
|
|
55
56
|
const key2 = await SymmetricEncryption.generateKey();
|
|
56
57
|
const value = "hello world";
|
|
57
58
|
const hmac1 = Hmac.create(key1, value);
|
|
58
59
|
const hmac2 = Hmac.create(key2, value);
|
|
59
|
-
|
|
60
|
+
assert.notStrictEqual(hmac1, hmac2);
|
|
60
61
|
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// test("should create same Hmacs for a given value and key", async () =>
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
// await test("should create same Hmacs for a given value and key", async () =>
|
|
65
66
|
// {
|
|
66
67
|
// let key = await SymmetricEncryption.generateKey();
|
|
67
68
|
// let hmac1 = await Hmac.create(key, "some-string");
|
|
68
69
|
// let hmac2 = await Hmac.create(key, "some-string");
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
//
|
|
70
|
+
// assert.ok(hmac1 !== null && !hmac1.isEmptyOrWhiteSpace());
|
|
71
|
+
// assert.ok(hmac2 !== null && !hmac2.isEmptyOrWhiteSpace());
|
|
72
|
+
// assert.notStrictEqual(hmac1, "some-string");
|
|
73
|
+
// assert.notStrictEqual(hmac2, "some-string");
|
|
74
|
+
// assert.strictEqual(hmac1, hmac2);
|
|
74
75
|
// });
|
|
75
|
-
|
|
76
|
-
// test("should throw CryptoException when key is null", async () =>
|
|
76
|
+
|
|
77
|
+
// await test("should throw CryptoException when key is null", async () =>
|
|
77
78
|
// {
|
|
78
79
|
// try
|
|
79
80
|
// {
|
|
@@ -81,14 +82,14 @@ suite("Hmac", () =>
|
|
|
81
82
|
// }
|
|
82
83
|
// catch (exception)
|
|
83
84
|
// {
|
|
84
|
-
//
|
|
85
|
-
//
|
|
85
|
+
// assert.ok(exception instanceof CryptoException);
|
|
86
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
86
87
|
// return;
|
|
87
88
|
// }
|
|
88
|
-
//
|
|
89
|
+
// assert.ok(false);
|
|
89
90
|
// });
|
|
90
|
-
|
|
91
|
-
// test("should throw CryptoException when value is null", async () =>
|
|
91
|
+
|
|
92
|
+
// await test("should throw CryptoException when value is null", async () =>
|
|
92
93
|
// {
|
|
93
94
|
// try
|
|
94
95
|
// {
|
|
@@ -97,14 +98,14 @@ suite("Hmac", () =>
|
|
|
97
98
|
// }
|
|
98
99
|
// catch (exception)
|
|
99
100
|
// {
|
|
100
|
-
//
|
|
101
|
-
//
|
|
101
|
+
// assert.ok(exception instanceof CryptoException);
|
|
102
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
102
103
|
// return;
|
|
103
104
|
// }
|
|
104
|
-
//
|
|
105
|
+
// assert.ok(false);
|
|
105
106
|
// });
|
|
106
|
-
|
|
107
|
-
// test("should throw CryptoException when key is undefined", async () =>
|
|
107
|
+
|
|
108
|
+
// await test("should throw CryptoException when key is undefined", async () =>
|
|
108
109
|
// {
|
|
109
110
|
// try
|
|
110
111
|
// {
|
|
@@ -112,14 +113,14 @@ suite("Hmac", () =>
|
|
|
112
113
|
// }
|
|
113
114
|
// catch (exception)
|
|
114
115
|
// {
|
|
115
|
-
//
|
|
116
|
-
//
|
|
116
|
+
// assert.ok(exception instanceof CryptoException);
|
|
117
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
117
118
|
// return;
|
|
118
119
|
// }
|
|
119
|
-
//
|
|
120
|
+
// assert.ok(false);
|
|
120
121
|
// });
|
|
121
122
|
|
|
122
|
-
// test("should throw CryptoException when value is undefined", async () =>
|
|
123
|
+
// await test("should throw CryptoException when value is undefined", async () =>
|
|
123
124
|
// {
|
|
124
125
|
// try
|
|
125
126
|
// {
|
|
@@ -128,14 +129,14 @@ suite("Hmac", () =>
|
|
|
128
129
|
// }
|
|
129
130
|
// catch (exception)
|
|
130
131
|
// {
|
|
131
|
-
//
|
|
132
|
-
//
|
|
132
|
+
// assert.ok(exception instanceof CryptoException);
|
|
133
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
133
134
|
// return;
|
|
134
135
|
// }
|
|
135
|
-
//
|
|
136
|
+
// assert.ok(false);
|
|
136
137
|
// });
|
|
137
|
-
|
|
138
|
-
// test("should throw CryptoException when invalid key", async () =>
|
|
138
|
+
|
|
139
|
+
// await test("should throw CryptoException when invalid key", async () =>
|
|
139
140
|
// {
|
|
140
141
|
// try
|
|
141
142
|
// {
|
|
@@ -143,12 +144,12 @@ suite("Hmac", () =>
|
|
|
143
144
|
// }
|
|
144
145
|
// catch (exception)
|
|
145
146
|
// {
|
|
146
|
-
//
|
|
147
|
-
//
|
|
147
|
+
// assert.ok(exception instanceof CryptoException);
|
|
148
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
148
149
|
// return;
|
|
149
150
|
// }
|
|
150
|
-
//
|
|
151
|
+
// assert.ok(false);
|
|
151
152
|
// });
|
|
152
153
|
});
|
|
153
|
-
|
|
154
|
+
|
|
154
155
|
});
|