mirror of
https://github.com/logos-messaging/logos-delivery-js.git
synced 2026-07-08 22:39:30 +00:00
* chore: update noise * update: package.lock * update: @chainsafe/libp2p-gossipsub * rm unwanted libp2p interface deps & bump up libp2p * refactor code for new deps * update: new package.lock * setup prettier, refactor eslint and rm trailing commas * update package.lock * fix build * import type for interface * fix imports for merge * update typedoc exports * add: CustomEvent import * use new libp2p interface * add aegir as dev dep for tests
34 lines
919 B
TypeScript
34 lines
919 B
TypeScript
import { Symmetric } from "../constants.js";
|
|
|
|
import { getSubtle, randomBytes } from "./index.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);
|
|
}
|