use typedarray everywhere and docs

This commit is contained in:
cupcakearmy 2022-10-14 13:52:18 +02:00
parent d3b9e9bc42
commit 7fa0cbfe93
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
5 changed files with 22 additions and 6 deletions

View File

@ -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++) {

View File

@ -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<string>
export async function hash(data: Uint8Array, hash: Hashes): Promise<Uint8Array>
export async function hash(data: string | Uint8Array, hash: Hashes): Promise<string | Uint8Array> {
export async function hash(data: TypedArray, hash: Hashes): Promise<TypedArray>
export async function 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)

View File

@ -1,6 +1,7 @@
import { type TypedArray } from '../utils/base.js'
import { getCrypto } from './crypto.js'
export async function getRandomBytes(bytes: number): Promise<Uint8Array> {
export async function getRandomBytes(bytes: number): Promise<TypedArray> {
if (bytes <= 0) throw new Error('Invalid number of bytes')
const buffer = new Uint8Array(bytes)

View File

@ -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'

View File

@ -3,5 +3,8 @@
"out": "./docs",
"name": "Occulto",
"includeVersion": true
"includeVersion": true,
"excludeInternal": true,
"excludePrivate": true
}