20 lines
635 B
TypeScript
20 lines
635 B
TypeScript
import { isBufferSource } from "./types.js";
|
|
import { BSON } from "bson";
|
|
import { type Bsonifiable, bsonify } from "@sammo/util";
|
|
import { isArray, isDate, isObject } from "lodash-es";
|
|
const subtle = globalThis.crypto.subtle;
|
|
|
|
export async function sha256(msg: Bsonifiable | BufferSource): Promise<ArrayBuffer> {
|
|
if (!isBufferSource(msg)) {
|
|
msg = BSON.serialize(bsonify(msg));
|
|
}
|
|
return await subtle.digest('SHA-256', msg);
|
|
}
|
|
|
|
export async function sha512(msg: Bsonifiable | BufferSource): Promise<ArrayBuffer> {
|
|
if (!isBufferSource(msg)) {
|
|
msg = BSON.serialize(bsonify(msg));
|
|
}
|
|
return await subtle.digest('SHA-512', msg);
|
|
}
|