Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Jul 15, 2024
1 parent 789b8ba commit 3f220b9
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 259 deletions.
48 changes: 0 additions & 48 deletions src/checksum.ts

This file was deleted.

34 changes: 34 additions & 0 deletions src/checksum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { concatBytes, equalsBytes, hexToBytes } from "ethereum-cryptography/utils";

import { IChecksumModule } from "../types";
import { sha256 } from "./sha256";

// default checksum configuration

export function defaultSha256Module(): Pick<IChecksumModule, "function"> {
return {
function: "sha256",
};
}

// checksum operations

function checksumData(key: Uint8Array, ciphertext: Uint8Array): Uint8Array {
return concatBytes(key.slice(16), ciphertext);
}

export async function checksum(mod: IChecksumModule, key: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array> {
if (mod.function === "sha256") {
return await sha256(checksumData(key, ciphertext));
} else {
throw new Error("Invalid checksum type");
}
}

export async function verifyChecksum(mod: IChecksumModule, key: Uint8Array, ciphertext: Uint8Array): Promise<boolean> {
if (mod.function === "sha256") {
return equalsBytes(hexToBytes(mod.message), await sha256(checksumData(key, ciphertext)));
} else {
throw new Error("Invalid checksum type");
}
}
13 changes: 13 additions & 0 deletions src/checksum/sha256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { sha256 as _sha256 } from "ethereum-cryptography/sha256";

import { hasWebCrypto } from "../env";

export const sha256 = hasWebCrypto ? sha256WebCrypto : sha256Js;

async function sha256WebCrypto(data: Uint8Array): Promise<Uint8Array> {
return new Uint8Array(await crypto.subtle.digest("SHA-256", data));
}

async function sha256Js(data: Uint8Array): Promise<Uint8Array> {
return _sha256(data);
}
91 changes: 0 additions & 91 deletions src/cipher.ts

This file was deleted.

60 changes: 60 additions & 0 deletions src/cipher/aes128Ctr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { encrypt as aesEncrypt, decrypt as aesDecrypt } from "ethereum-cryptography/aes";

import { hasWebCrypto } from "../env";

export const aes128CtrEncrypt = hasWebCrypto ? aes128CtrEncryptWebCrypto : aes128CtrEncryptJs;
export const aes128CtrDecrypt = hasWebCrypto ? aes128CtrDecryptWebCrypto : aes128CtrDecryptJs;

export async function aes128CtrEncryptJs(key: Uint8Array, iv: Uint8Array, data: Uint8Array): Promise<Uint8Array> {
return await aesEncrypt(
data,
key,
iv,
"aes-128-ctr",
false,
);
}

async function aes128CtrEncryptWebCrypto(
key: Uint8Array,
iv: Uint8Array,
data: Uint8Array
): Promise<Uint8Array> {
const cryptoKey = await crypto.subtle.importKey(
"raw",
key,
{name: "AES-CTR"},
false,
["encrypt"]
);
return new Uint8Array(await crypto.subtle.encrypt(
{ name: "AES-CTR", counter: iv, length: 128 },
cryptoKey,
data
));
}

export async function aes128CtrDecryptJs(key: Uint8Array, iv: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array> {
return await aesDecrypt(
ciphertext,
key,
iv,
"aes-128-ctr",
false,
);
}

async function aes128CtrDecryptWebCrypto(key: Uint8Array, iv: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array> {
const cryptoKey = await crypto.subtle.importKey(
"raw",
key,
{name: "AES-CTR"},
false,
["decrypt"]
);
return new Uint8Array(await crypto.subtle.decrypt(
{ name: "AES-CTR", counter: iv, length: 128 },
cryptoKey,
ciphertext
));
}
38 changes: 38 additions & 0 deletions src/cipher/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getRandomBytesSync } from "ethereum-cryptography/random";
import { bytesToHex, hexToBytes } from "ethereum-cryptography/utils";

import { ICipherModule } from "../types";
import { aes128CtrDecrypt, aes128CtrEncrypt } from "./aes128Ctr";

export function defaultAes128CtrModule(): Pick<ICipherModule, "function" | "params"> {
return {
function: "aes-128-ctr",
params: {
iv: bytesToHex(getRandomBytesSync(16)),
},
};
}

export async function cipherEncrypt(mod: ICipherModule, key: Uint8Array, data: Uint8Array): Promise<Uint8Array> {
if (mod.function === "aes-128-ctr") {
try {
return await aes128CtrEncrypt(key, hexToBytes(mod.params.iv), data);
} catch (e) {
throw new Error("Unable to encrypt");
}
} else {
throw new Error("Invalid cipher type");
}
}

export async function cipherDecrypt(mod: ICipherModule, key: Uint8Array): Promise<Uint8Array> {
if (mod.function === "aes-128-ctr") {
try {
return await aes128CtrDecrypt(key, hexToBytes(mod.params.iv), hexToBytes(mod.message));
} catch (e) {
throw new Error("Unable to decrypt")
}
} else {
throw new Error("Invalid cipher type");
}
}
Loading

0 comments on commit 3f220b9

Please sign in to comment.