mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-26 09:33:10 +00:00
* export crypto primitives * export crypto * update imports * fix size limit * rename crypto.js * move Signature type * fix path * fix: size-limit (#1734) * fix paths, revert change to config --------- Co-authored-by: Danish Arora <35004822+danisharora099@users.noreply.github.com>
34 lines
914 B
TypeScript
34 lines
914 B
TypeScript
import { Symmetric } from "../misc.js";
|
|
|
|
import { getSubtle, randomBytes } from "./utils.js";
|
|
|
|
export async function encrypt(
|
|
iv: Uint8Array,
|
|
key: Uint8Array,
|
|
clearText: Uint8Array
|
|
): Promise<Uint8Array> {
|
|
return getSubtle()
|
|
.importKey("raw", key, Symmetric.algorithm, false, ["encrypt"])
|
|
.then((cryptoKey) =>
|
|
getSubtle().encrypt({ iv, ...Symmetric.algorithm }, cryptoKey, clearText)
|
|
)
|
|
.then((cipher) => new Uint8Array(cipher));
|
|
}
|
|
|
|
export async function decrypt(
|
|
iv: Uint8Array,
|
|
key: Uint8Array,
|
|
cipherText: Uint8Array
|
|
): Promise<Uint8Array> {
|
|
return getSubtle()
|
|
.importKey("raw", key, Symmetric.algorithm, false, ["decrypt"])
|
|
.then((cryptoKey) =>
|
|
getSubtle().decrypt({ iv, ...Symmetric.algorithm }, cryptoKey, cipherText)
|
|
)
|
|
.then((clear) => new Uint8Array(clear));
|
|
}
|
|
|
|
export function generateIv(): Uint8Array {
|
|
return randomBytes(Symmetric.ivSize);
|
|
}
|