diff --git a/src/crypto/encoding.ts b/src/crypto/encoding.ts index a0b00e1..b1090a0 100644 --- a/src/crypto/encoding.ts +++ b/src/crypto/encoding.ts @@ -25,7 +25,7 @@ export class Base64 { } export class Hex { - static encode(buffer: Uint8Array): string { + static encode(buffer: TypedArray): string { let s = '' for (const i of new Uint8Array(buffer)) { s += i.toString(16).padStart(2, '0') @@ -33,7 +33,7 @@ export class Hex { return s } - static decode(s: string): Uint8Array { + static decode(s: string): TypedArray { const size = s.length / 2 const buffer = new Uint8Array(size) for (let i = 0; i < size; i++) { diff --git a/src/crypto/hash.ts b/src/crypto/hash.ts index fd2f76d..64a965b 100644 --- a/src/crypto/hash.ts +++ b/src/crypto/hash.ts @@ -1,7 +1,18 @@ +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', @@ -9,8 +20,8 @@ export enum Hashes { } export async function hash(data: string, hash: Hashes): Promise -export async function hash(data: Uint8Array, hash: Hashes): Promise -export async function hash(data: string | Uint8Array, hash: Hashes): Promise { +export async function hash(data: TypedArray, hash: Hashes): Promise +export async function hash(data: string | TypedArray, hash: Hashes): Promise { const isString = typeof data === 'string' const c = await getCrypto() const result = await c.subtle.digest(hash, isString ? Bytes.encode(data) : data) diff --git a/src/crypto/random.ts b/src/crypto/random.ts index 75cd258..3e73d89 100644 --- a/src/crypto/random.ts +++ b/src/crypto/random.ts @@ -1,6 +1,7 @@ +import { type TypedArray } from '../utils/base.js' import { getCrypto } from './crypto.js' -export async function getRandomBytes(bytes: number): Promise { +export async function getRandomBytes(bytes: number): Promise { if (bytes <= 0) throw new Error('Invalid number of bytes') const buffer = new Uint8Array(bytes) diff --git a/src/index.ts b/src/index.ts index 543086d..e8bbdc2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ export { Base64, Bytes, Hex } from './crypto/encoding.js' export { hash, Hashes } from './crypto/hash.js' export { getRandomBytes } from './crypto/random.js' +export { TypedArray } from './utils/base.js' diff --git a/typedoc.json b/typedoc.json index c1484d4..1fdad13 100644 --- a/typedoc.json +++ b/typedoc.json @@ -3,5 +3,8 @@ "out": "./docs", "name": "Occulto", - "includeVersion": true + "includeVersion": true, + + "excludeInternal": true, + "excludePrivate": true }