mirror of
https://github.com/cupcakearmy/occulto.git
synced 2024-12-22 04:06:29 +00:00
use typedarray everywhere and docs
This commit is contained in:
parent
d3b9e9bc42
commit
7fa0cbfe93
@ -25,7 +25,7 @@ export class Base64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Hex {
|
export class Hex {
|
||||||
static encode(buffer: Uint8Array): string {
|
static encode(buffer: TypedArray): string {
|
||||||
let s = ''
|
let s = ''
|
||||||
for (const i of new Uint8Array(buffer)) {
|
for (const i of new Uint8Array(buffer)) {
|
||||||
s += i.toString(16).padStart(2, '0')
|
s += i.toString(16).padStart(2, '0')
|
||||||
@ -33,7 +33,7 @@ export class Hex {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
static decode(s: string): Uint8Array {
|
static decode(s: string): TypedArray {
|
||||||
const size = s.length / 2
|
const size = s.length / 2
|
||||||
const buffer = new Uint8Array(size)
|
const buffer = new Uint8Array(size)
|
||||||
for (let i = 0; i < size; i++) {
|
for (let i = 0; i < size; i++) {
|
||||||
|
@ -1,7 +1,18 @@
|
|||||||
|
import { type TypedArray } from '../utils/base.js'
|
||||||
import { getCrypto } from './crypto.js'
|
import { getCrypto } from './crypto.js'
|
||||||
import { Bytes, Hex } from './encoding.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 {
|
export enum Hashes {
|
||||||
|
/**
|
||||||
|
* @remarks SHA-1 is not recommended for new applications as it's not cryptographically secure.
|
||||||
|
*/
|
||||||
SHA_1 = 'SHA-1',
|
SHA_1 = 'SHA-1',
|
||||||
SHA_256 = 'SHA-256',
|
SHA_256 = 'SHA-256',
|
||||||
SHA_384 = 'SHA-384',
|
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: string, hash: Hashes): Promise<string>
|
||||||
export async function hash(data: Uint8Array, hash: Hashes): Promise<Uint8Array>
|
export async function hash(data: TypedArray, hash: Hashes): Promise<TypedArray>
|
||||||
export async function hash(data: string | Uint8Array, hash: Hashes): Promise<string | Uint8Array> {
|
export async function hash(data: string | TypedArray, hash: Hashes): Promise<string | TypedArray> {
|
||||||
const isString = typeof data === 'string'
|
const isString = typeof data === 'string'
|
||||||
const c = await getCrypto()
|
const c = await getCrypto()
|
||||||
const result = await c.subtle.digest(hash, isString ? Bytes.encode(data) : data)
|
const result = await c.subtle.digest(hash, isString ? Bytes.encode(data) : data)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
import { type TypedArray } from '../utils/base.js'
|
||||||
import { getCrypto } from './crypto.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')
|
if (bytes <= 0) throw new Error('Invalid number of bytes')
|
||||||
|
|
||||||
const buffer = new Uint8Array(bytes)
|
const buffer = new Uint8Array(bytes)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
export { Base64, Bytes, Hex } from './crypto/encoding.js'
|
export { Base64, Bytes, Hex } from './crypto/encoding.js'
|
||||||
export { hash, Hashes } from './crypto/hash.js'
|
export { hash, Hashes } from './crypto/hash.js'
|
||||||
export { getRandomBytes } from './crypto/random.js'
|
export { getRandomBytes } from './crypto/random.js'
|
||||||
|
export { TypedArray } from './utils/base.js'
|
||||||
|
@ -3,5 +3,8 @@
|
|||||||
"out": "./docs",
|
"out": "./docs",
|
||||||
|
|
||||||
"name": "Occulto",
|
"name": "Occulto",
|
||||||
"includeVersion": true
|
"includeVersion": true,
|
||||||
|
|
||||||
|
"excludeInternal": true,
|
||||||
|
"excludePrivate": true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user