Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoalh committed Dec 16, 2024
1 parent 77c9fbc commit 608e613
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,11 @@ export class SymmetricCryptor {
this.#decoder = decoder;
this.#encoder = encoder;
}
/**
* Decrypt data.
* @param {string} data Data that need to symmetric decrypt.
* @returns {Promise<string>} A decrypted data.
*/
async decrypt(data: string): Promise<string>;
/**
* Decrypt data.
* @param {Uint8Array} data Data that need to symmetric decrypt.
* @returns {Promise<Uint8Array>} A decrypted data.
*/
async decrypt(data: Uint8Array): Promise<Uint8Array>;
async decrypt(data: string | Uint8Array): Promise<string | Uint8Array> {
async #decrypt(data: Uint8Array): Promise<Uint8Array> {
if (data.length === 0) {
return data;
}
const resultIsString: boolean = typeof data === "string";
let storage: Uint8Array = resultIsString ? this.#decoder(data as string) : (data as Uint8Array);
let storage: Uint8Array = new Uint8Array(data);
for (const {
algorithm,
key
Expand Down Expand Up @@ -111,7 +98,25 @@ export class SymmetricCryptor {
}
storage = new Uint8Array(await crypto.subtle.decrypt(decryptParameterAlgorithm, key, decryptParameterData));
}
return (resultIsString ? new TextDecoder().decode(storage) : storage);
return storage;
}
/**
* Decrypt data.
* @param {string} data Data that need to symmetric decrypt.
* @returns {Promise<string>} A decrypted data.
*/
async decrypt(data: string): Promise<string>;
/**
* Decrypt data.
* @param {Uint8Array} data Data that need to symmetric decrypt.
* @returns {Promise<Uint8Array>} A 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(this.#decoder(data)));
}
return this.#decrypt(data);
}
/**
* Decrypt files in place. All of the files will not decrypted if any file fail to decrypt.
Expand All @@ -129,7 +134,7 @@ export class SymmetricCryptor {
const result: SymmetricCryptorInternalFileIO[] = await Promise.all(filesPath.map(async (filePath: string | URL): Promise<SymmetricCryptorInternalFileIO> => {
try {
return {
context: await this.decrypt(await Deno.readFile(filePath)),
context: await this.#decrypt(await Deno.readFile(filePath)),
filePath
};
} catch (error) {
Expand All @@ -156,24 +161,11 @@ export class SymmetricCryptor {
* @deprecated Migrate to {@linkcode SymmetricCryptor.decryptFile}.
*/
decryptFiles: (...filesPath: (string | URL)[]) => Promise<this> = this.decryptFile;
/**
* Encrypt data.
* @param {string} data Data that need to symmetric encrypt.
* @returns {Promise<string>} An encrypted data.
*/
async encrypt(data: string): Promise<string>;
/**
* Encrypt data.
* @param {Uint8Array} data Data that need to symmetric encrypt.
* @returns {Promise<Uint8Array>} An encrypted data.
*/
async encrypt(data: Uint8Array): Promise<Uint8Array>;
async encrypt(data: string | Uint8Array): Promise<string | Uint8Array> {
async #encrypt(data: Uint8Array): Promise<Uint8Array> {
if (data.length === 0) {
return data;
}
const resultIsString: boolean = typeof data === "string";
let storage: Uint8Array = resultIsString ? new TextEncoder().encode(data as string) : (data as Uint8Array);
let storage: Uint8Array = new Uint8Array(data);
for (const {
algorithm,
key
Expand Down Expand Up @@ -208,7 +200,25 @@ export class SymmetricCryptor {
}
storage = Uint8Array.from([...token, ...new Uint8Array(await crypto.subtle.encrypt(encryptParameterAlgorithm, key, storage))]);
}
return (resultIsString ? this.#encoder(storage) : storage);
return storage;
}
/**
* Encrypt data.
* @param {string} data Data that need to symmetric encrypt.
* @returns {Promise<string>} An encrypted data.
*/
async encrypt(data: string): Promise<string>;
/**
* Encrypt data.
* @param {Uint8Array} data Data that need to symmetric encrypt.
* @returns {Promise<Uint8Array>} An 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 files in place. All of the files will not encrypted if any file fail to encrypt.
Expand All @@ -226,7 +236,7 @@ export class SymmetricCryptor {
const result: SymmetricCryptorInternalFileIO[] = await Promise.all(filesPath.map(async (filePath: string | URL): Promise<SymmetricCryptorInternalFileIO> => {
try {
return {
context: await this.encrypt(await Deno.readFile(filePath)),
context: await this.#encrypt(await Deno.readFile(filePath)),
filePath
};
} catch (error) {
Expand Down

0 comments on commit 608e613

Please sign in to comment.