js-waku/src/lib/crypto.ts
Franck R 2798376776
Remove ecies-geth (#598)
* test: specify encryption method

Makes debugging easier.

* Fix log typo

* Remove ecies-geth

Start removal of elliptic dependency and move towards exclusive usage to
CryptoSubtle.
2022-03-06 23:20:59 +11:00

30 lines
664 B
TypeScript

import nodeCrypto from "crypto";
// IE 11
declare global {
interface Window {
msCrypto?: Crypto;
}
interface Crypto {
webkitSubtle?: SubtleCrypto;
}
}
const crypto = window.crypto || window.msCrypto || nodeCrypto.webcrypto;
const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;
if (subtle === undefined) {
throw new Error("crypto and/or subtle api unavailable");
}
export { crypto, subtle };
export function randomBytes(size: number): Uint8Array {
return crypto.getRandomValues(new Uint8Array(size));
}
export function sha256(msg: ArrayBufferLike): Promise<ArrayBuffer> {
return subtle.digest({ name: "SHA-256" }, msg);
}