mirror of
https://github.com/cupcakearmy/occulto.git
synced 2025-09-07 16:20:40 +00:00
* rewrite * testing worklflow * sprcify version * config stuff * add hash as buffer * delete docs * use typedarray everywhere and docs * readme * aes * cleanup * rsa * testing with playwright * fix playwright * readme * docs * update deps * use headless * add prepublish * build pipeline * move types up * move types up * add types legacy * packaging * versions bump * cleanup * maybe this time * drop support for commonjs * version bump * cleanup
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
import { type TypedArray } from '../utils/base.js'
|
|
import { getCrypto } from './crypto.js'
|
|
import { Bytes, Hex } from './encoding.js'
|
|
|
|
/**
|
|
* List of available hash functions.
|
|
*
|
|
* @remarks
|
|
* For cryptographic details refer to: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#supported_algorithms
|
|
* Reference: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
|
|
*/
|
|
export enum Hashes {
|
|
/**
|
|
* @remarks SHA-1 is not recommended for new applications as it's not cryptographically secure.
|
|
*/
|
|
SHA_1 = 'SHA-1',
|
|
SHA_256 = 'SHA-256',
|
|
SHA_384 = 'SHA-384',
|
|
SHA_512 = 'SHA-512',
|
|
}
|
|
|
|
export class Hash {
|
|
static async hash(data: string, hash: Hashes): Promise<string>
|
|
static async hash(data: TypedArray, hash: Hashes): Promise<TypedArray>
|
|
static async hash(data: string | TypedArray, hash: Hashes): Promise<string | TypedArray> {
|
|
const isString = typeof data === 'string'
|
|
const c = await getCrypto()
|
|
const result = await c.subtle.digest(hash, isString ? Bytes.encode(data) : data)
|
|
const buf = new Uint8Array(result)
|
|
return isString ? Hex.encode(buf) : buf
|
|
}
|
|
}
|