-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
416 lines (416 loc) · 15.1 KB
/
mod.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import {
decodeAscii85,
encodeAscii85
} from "jsr:@std/encoding@^1.0.5";
/**
* Enum of the algorithm of the symmetric cryptor.
*/
export enum SymmetricCryptorAlgorithm {
"AES-CBC" = "AES-CBC",
"AES-CTR" = "AES-CTR",
"AES-GCM" = "AES-GCM",
AESCBC = "AES-CBC",
AESCTR = "AES-CTR",
AESGCM = "AES-GCM"
}
export type SymmetricCryptorCipherTextDecoder = (input: string) => Uint8Array | Promise<Uint8Array>;
export type SymmetricCryptorCipherTextEncoder = (input: Uint8Array) => string | Promise<string>;
/**
* Type of the key of the symmetric cryptor.
*/
export type SymmetricCryptorKeyType = string | ArrayBuffer | DataView | Uint8Array | Uint16Array | Uint32Array | BigUint64Array;
/**
* Input of the key of the symmetric cryptor.
*/
export interface SymmetricCryptorKeyInput {
/**
* Algorithm of the symmetric cryptor.
* @default {"AES-CBC"}
*/
algorithm?: SymmetricCryptorAlgorithm | keyof typeof SymmetricCryptorAlgorithm;
/**
* Key of the symmetric cryptor.
*/
key: SymmetricCryptorKeyType;
}
interface SymmetricCryptorKey {
algorithm: `${SymmetricCryptorAlgorithm}`;
key: CryptoKey;
}
interface SymmetricCryptorFileIO {
context: Uint8Array;
filePath: string | URL;
}
/**
* A password based cryptor.
*/
export class SymmetricCryptor {
get [Symbol.toStringTag](): string {
return "SymmetricCryptor";
}
#cryptors: readonly SymmetricCryptorKey[];
#decoder: SymmetricCryptorCipherTextDecoder;
#encoder: SymmetricCryptorCipherTextEncoder;
private constructor(cryptors: readonly SymmetricCryptorKey[], decoder: SymmetricCryptorCipherTextDecoder, encoder: SymmetricCryptorCipherTextEncoder) {
this.#cryptors = cryptors;
this.#decoder = decoder;
this.#encoder = encoder;
}
async #decrypt(data: Uint8Array): Promise<Uint8Array> {
if (data.length === 0) {
return data;
}
let storage: Uint8Array = new Uint8Array(data);
for (const {
algorithm,
key
} of [...this.#cryptors].reverse()) {
let decryptParameterAlgorithm: AlgorithmIdentifier | AesCbcParams | AesCtrParams | AesGcmParams;
let decryptParameterData: BufferSource;
switch (algorithm) {
case "AES-CBC":
decryptParameterAlgorithm = {
name: algorithm,
iv: storage.slice(0, 16)
};
decryptParameterData = storage.slice(16);
break;
case "AES-CTR":
decryptParameterAlgorithm = {
name: algorithm,
counter: storage.slice(0, 16),
length: 64
};
decryptParameterData = storage.slice(16);
break;
case "AES-GCM":
decryptParameterAlgorithm = {
name: algorithm,
iv: storage.slice(0, 12)
};
decryptParameterData = storage.slice(12);
break;
default:
throw new Error(`\`${algorithm}\` is not a symmetric crypto algorithm which able to process! How did you get to here?`);
}
storage = new Uint8Array(await crypto.subtle.decrypt(decryptParameterAlgorithm, key, decryptParameterData));
}
return storage;
}
/**
* Decrypt the data.
* @param {string} data Data that need to decrypt.
* @returns {Promise<string>} The decrypted data.
*/
async decrypt(data: string): Promise<string>;
/**
* Decrypt the data.
* @param {Uint8Array} data Data that need to decrypt.
* @returns {Promise<Uint8Array>} The decrypted data.
*/
async decrypt(data: Uint8Array): Promise<Uint8Array>;
async decrypt(data: string | Uint8Array): Promise<string | Uint8Array> {
if (typeof data === "string") {
return new TextDecoder().decode(await this.#decrypt(await this.#decoder(data)));
}
return this.#decrypt(data);
}
/**
* Decrypt the files in place. All of the files will not decrypted if any file fail to decrypt.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
* > - *Resources*
* > - File System - Write \[Deno: `write`; NodeJS (>= v20.9.0) 🧪: `fs-write`\]
* > - *Resources*
* @param {...(string | URL)} filesPath Path of the files.
* @returns {Promise<this>}
*/
async decryptFile(...filesPath: (string | URL)[]): Promise<this> {
const result: SymmetricCryptorFileIO[] = await Promise.all(filesPath.map(async (filePath: string | URL): Promise<SymmetricCryptorFileIO> => {
try {
return {
context: await this.#decrypt(await Deno.readFile(filePath)),
filePath
};
} catch (error) {
throw new Error(`Unable to decrypt the file \`${filePath}\`: ${(error as Error)?.message ?? error}`);
}
}));
await Promise.all(result.map(({
context,
filePath
}: SymmetricCryptorFileIO): Promise<void> => {
return Deno.writeFile(filePath, context, { create: false });
}));
return this;
}
/**
* Decrypt the files in place. All of the files will not decrypted if any file fail to decrypt.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
* > - *Resources*
* > - File System - Write \[Deno: `write`; NodeJS (>= v20.9.0) 🧪: `fs-write`\]
* > - *Resources*
* @deprecated Migrate to {@linkcode SymmetricCryptor.decryptFile}.
*/
decryptFiles: (...filesPath: (string | URL)[]) => Promise<this> = this.decryptFile;
async #encrypt(data: Uint8Array): Promise<Uint8Array> {
if (data.length === 0) {
return data;
}
let storage: Uint8Array = new Uint8Array(data);
for (const {
algorithm,
key
} of this.#cryptors) {
let encryptParameterAlgorithm: AlgorithmIdentifier | AesCbcParams | AesCtrParams | AesGcmParams;
let token: Uint8Array;
switch (algorithm) {
case "AES-CBC":
token = crypto.getRandomValues(new Uint8Array(16));
encryptParameterAlgorithm = {
name: algorithm,
iv: token
};
break;
case "AES-CTR":
token = crypto.getRandomValues(new Uint8Array(16));
encryptParameterAlgorithm = {
name: algorithm,
counter: token,
length: 64
};
break;
case "AES-GCM":
token = crypto.getRandomValues(new Uint8Array(12));
encryptParameterAlgorithm = {
name: algorithm,
iv: token
};
break;
default:
throw new Error(`\`${algorithm}\` is not a symmetric crypto algorithm which able to process! How did you get to here?`);
}
storage = Uint8Array.from([...token, ...new Uint8Array(await crypto.subtle.encrypt(encryptParameterAlgorithm, key, storage))]);
}
return storage;
}
/**
* Encrypt the data.
* @param {string} data Data that need to encrypt.
* @returns {Promise<string>} The encrypted data.
*/
async encrypt(data: string): Promise<string>;
/**
* Encrypt the data.
* @param {Uint8Array} data Data that need to encrypt.
* @returns {Promise<Uint8Array>} The encrypted data.
*/
async encrypt(data: Uint8Array): Promise<Uint8Array>;
async encrypt(data: string | Uint8Array): Promise<string | Uint8Array> {
if (typeof data === "string") {
return this.#encoder(await this.#encrypt(new TextEncoder().encode(data)));
}
return this.#encrypt(data);
}
/**
* Encrypt the files in place. All of the files will not encrypted if any file fail to encrypt.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
* > - *Resources*
* > - File System - Write \[Deno: `write`; NodeJS (>= v20.9.0) 🧪: `fs-write`\]
* > - *Resources*
* @param {...(string | URL)} filesPath Path of the files.
* @returns {Promise<this>}
*/
async encryptFile(...filesPath: (string | URL)[]): Promise<this> {
const result: SymmetricCryptorFileIO[] = await Promise.all(filesPath.map(async (filePath: string | URL): Promise<SymmetricCryptorFileIO> => {
try {
return {
context: await this.#encrypt(await Deno.readFile(filePath)),
filePath
};
} catch (error) {
throw new Error(`Unable to encrypt the file \`${filePath}\`: ${(error as Error)?.message ?? error}`);
}
}));
await Promise.all(result.map(({
context,
filePath
}: SymmetricCryptorFileIO): Promise<void> => {
return Deno.writeFile(filePath, context, { create: false });
}));
return this;
}
/**
* Encrypt the files in place. All of the files will not encrypted if any file fail to encrypt.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
* > - *Resources*
* > - File System - Write \[Deno: `write`; NodeJS (>= v20.9.0) 🧪: `fs-write`\]
* > - *Resources*
* @deprecated Migrate to {@linkcode SymmetricCryptor.encryptFile}.
*/
encryptFiles: (...filesPath: (string | URL)[]) => Promise<this> = this.encryptFile;
/**
* Read the encrypted file.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
* > - *Resources*
* @param {string | URL} filePath Path of the file.
* @param {Deno.ReadFileOptions} [options={}] Options.
* @returns {Promise<Uint8Array>} Decrypted data of the file.
*/
async readEncryptedFile(filePath: string | URL, options?: Deno.ReadFileOptions): Promise<Uint8Array> {
return this.#decrypt(await Deno.readFile(filePath, options));
}
/**
* Read the encrypted text file.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
* > - *Resources*
* @param {string | URL} filePath Path of the file.
* @param {Deno.ReadFileOptions} [options={}] Options.
* @returns {Promise<string>} Decrypted text data of the file.
*/
async readEncryptedTextFile(filePath: string | URL, options?: Deno.ReadFileOptions): Promise<string> {
return new TextDecoder().decode(await this.readEncryptedFile(filePath, options));
}
/**
* Write the encrypted file.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Write \[Deno: `write`; NodeJS (>= v20.9.0) 🧪: `fs-write`\]
* > - *Resources*
* @param {string | URL} filePath Path of the file.
* @param {Uint8Array} data Data of the file.
* @param {Omit<Deno.WriteFileOptions, "append">} [options={}] Options.
* @returns {Promise<this>}
*/
async writeEncryptedFile(filePath: string | URL, data: Uint8Array, options?: Omit<Deno.WriteFileOptions, "append">): Promise<this> {
await Deno.writeFile(filePath, await this.#encrypt(data), {
...options,
append: false
});
return this;
}
/**
* Write the encrypted text file.
*
* > **🛡️ Runtime Permissions**
* >
* > - File System - Write \[Deno: `write`; NodeJS (>= v20.9.0) 🧪: `fs-write`\]
* > - *Resources*
* @param {string | URL} filePath Path of the file.
* @param {string} data Text data of the file.
* @param {Omit<Deno.WriteFileOptions, "append">} [options={}] Options.
* @returns {Promise<this>}
*/
async writeEncryptedTextFile(filePath: string | URL, data: string, options?: Omit<Deno.WriteFileOptions, "append">): Promise<this> {
await this.writeEncryptedFile(filePath, new TextEncoder().encode(data), options);
return this;
}
}
async function createCryptorKey(input: SymmetricCryptorKeyInput | SymmetricCryptorKeyType): Promise<SymmetricCryptorKey> {
let algorithm: `${SymmetricCryptorAlgorithm}`;
let key: SymmetricCryptorKeyType;
if (
typeof input === "string" ||
input instanceof ArrayBuffer ||
input instanceof DataView ||
input instanceof Uint8Array ||
input instanceof Uint16Array ||
input instanceof Uint32Array ||
input instanceof BigUint64Array
) {
algorithm = "AES-CBC";
key = input;
} else {
const algorithmFmt: `${SymmetricCryptorAlgorithm}` | undefined = SymmetricCryptorAlgorithm[input.algorithm ?? "AES-CBC"];
if (typeof algorithmFmt === "undefined") {
throw new RangeError(`\`${input.algorithm}\` is not a valid symmetric crypto algorithm! Only accept these values: ${Array.from(new Set<string>(Object.keys(SymmetricCryptorAlgorithm)).values()).sort().join(", ")}`);
}
algorithm = algorithmFmt;
key = input.key;
}
return {
algorithm,
key: await crypto.subtle.importKey("raw", await crypto.subtle.digest("SHA-256", (typeof key === "string") ? new TextEncoder().encode(key) : key), { name: algorithm }, false, ["decrypt", "encrypt"])
};
}
export interface SymmetricCryptorOptions {
/**
* Decoder of the stringify cipher text, must also define and exchangeable with property {@linkcode encoder}. Default to use the ASCII85 decoder.
*/
decoder?: SymmetricCryptorCipherTextDecoder;
/**
* Encoder of the stringify cipher text, must also define and exchangeable with property {@linkcode decoder}. Default to use the ASCII85 encoder.
*/
encoder?: SymmetricCryptorCipherTextEncoder;
/**
* Times of the crypto.
* @default {1}
*/
times?: number;
}
/**
* Create an instance of the class {@linkcode SymmetricCryptor}.
* @param {SymmetricCryptorKeyInput | SymmetricCryptorKeyType} key Key of the symmetric cryptor.
* @param {SymmetricCryptorOptions} [options={}] Options of the symmetric cryptor.
* @returns {Promise<SymmetricCryptor>} An instance of the class {@linkcode SymmetricCryptor}.
*/
export async function createSymmetricCryptor(key: SymmetricCryptorKeyInput | SymmetricCryptorKeyType, options?: SymmetricCryptorOptions): Promise<SymmetricCryptor>;
/**
* Create an instance of the class {@linkcode SymmetricCryptor}.
* @param {(SymmetricCryptorKeyInput | SymmetricCryptorKeyType)[]} keys Keys of the symmetric cryptor.
* @param {Omit<SymmetricCryptorOptions, "times">} [options={}] Options of the symmetric cryptor.
* @returns {Promise<SymmetricCryptor>} An instance of the class {@linkcode SymmetricCryptor}.
*/
export async function createSymmetricCryptor(keys: (SymmetricCryptorKeyInput | SymmetricCryptorKeyType)[], options?: Omit<SymmetricCryptorOptions, "times">): Promise<SymmetricCryptor>;
export async function createSymmetricCryptor(param0: SymmetricCryptorKeyInput | SymmetricCryptorKeyType | (SymmetricCryptorKeyInput | SymmetricCryptorKeyType)[], options: SymmetricCryptorOptions = {}): Promise<SymmetricCryptor> {
if (!(
(typeof options.decoder !== "undefined" && typeof options.encoder !== "undefined") ||
(typeof options.decoder === "undefined" && typeof options.encoder === "undefined")
)) {
throw new ReferenceError(`Parameters \`options.decoder\` and \`options.encoder\` are not all defined or all undefined!`);
}
const decoder: SymmetricCryptorCipherTextDecoder = options.decoder ?? decodeAscii85;
const encoder: SymmetricCryptorCipherTextEncoder = options.encoder ?? encodeAscii85;
const cryptors: SymmetricCryptorKey[] = [];
if (Array.isArray(param0)) {
cryptors.push(...await Promise.all(param0.map((input: SymmetricCryptorKeyInput | SymmetricCryptorKeyType): Promise<SymmetricCryptorKey> => {
return createCryptorKey(input);
})));
} else {
const cryptor: SymmetricCryptorKey = await createCryptorKey(param0);
if (typeof options.times === "undefined") {
cryptors.push(cryptor);
} else {
if (!(Number.isSafeInteger(options.times) && options.times >= 1)) {
throw new TypeError(`\`${options.times}\` (parameter \`options.times\`) is not a number which is integer, safe, and >= 1!`);
}
for (let index: number = 0; index < options.times; index += 1) {
cryptors.push(cryptor);
}
}
}
if (cryptors.length > 0) {
//@ts-ignore Access private constructor.
return new SymmetricCryptor(cryptors, decoder, encoder);
}
throw new ReferenceError(`Parameter \`keys\` is not defined!`);
}
export default createSymmetricCryptor;