-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
789b8ba
commit 3f220b9
Showing
10 changed files
with
270 additions
and
259 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.