Use `@noble/secp2156k1`'s random byte util

This commit is contained in:
Franck Royer 2022-05-10 10:47:13 +10:00
parent 0e42cf6d7f
commit 9829cc2cab
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 2 additions and 12 deletions

View File

@ -1,5 +1,6 @@
import nodeCrypto from "crypto";
import * as secp from "@noble/secp256k1";
import { concat } from "uint8arrays/concat";
declare const self: Record<string, any> | undefined;
@ -20,18 +21,7 @@ export function getSubtle(): SubtleCrypto {
}
}
export function randomBytes(bytesLength = 32): Uint8Array {
if (crypto.web) {
return crypto.web.getRandomValues(new Uint8Array(bytesLength));
} else if (crypto.node) {
const { randomBytes } = crypto.node;
return Uint8Array.from(randomBytes(bytesLength));
} else {
throw new Error(
"The environment doesn't have randomBytes function (if in the browser, be sure to use to be in a secure context, ie, https)"
);
}
}
export const randomBytes = secp.utils.randomBytes;
export async function sha256(...messages: Uint8Array[]): Promise<Uint8Array> {
if (crypto.web) {