From 91693e55e1f81e293ebb31c5bcc97cb9146ab5c5 Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Mon, 3 Feb 2025 10:29:33 +0100 Subject: [PATCH] cleanup types --- src/crypto/aes.ts | 17 ++++++++--------- src/crypto/encoding.ts | 14 +++++++------- src/crypto/hash.ts | 5 ++--- src/crypto/random.ts | 3 +-- src/crypto/rsa.ts | 10 +++++----- src/index.ts | 1 - src/utils/base.ts | 11 ----------- 7 files changed, 23 insertions(+), 38 deletions(-) diff --git a/src/crypto/aes.ts b/src/crypto/aes.ts index f9d8fec..cdf6cc2 100644 --- a/src/crypto/aes.ts +++ b/src/crypto/aes.ts @@ -1,4 +1,3 @@ -import { type TypedArray } from '../utils/base.js' import { getCrypto } from './crypto.js' import { Base64, Bytes } from './encoding.js' import { Hashes } from './hash.js' @@ -15,7 +14,7 @@ export type KeyData = { name: 'PBKDF2' hash: Hashes iterations: number - salt: TypedArray + salt: ArrayBufferLike length: number } @@ -35,12 +34,12 @@ export class AES { private static InvalidCiphertext = new Error('Invalid ciphertext') - private static async join(...args: TypedArray[]): Promise { + private static async join(...args: ArrayBufferLike[]): Promise { const strings = await Promise.all(args.map(Base64.encode)) return strings.join(AES.delimiter) } - private static async split(ciphertext: string): Promise { + private static async split(ciphertext: string): Promise { const splitted = ciphertext.split(AES.delimiter) return Promise.all(splitted.map(Base64.decode)) } @@ -49,7 +48,7 @@ export class AES { * Derive a key from a password. * To be used if the password is not 128, 192 or 256 bits or human made, non generated keys. */ - static async derive(key: string, options?: KeyData): Promise<[TypedArray, KeyData]> { + static async derive(key: string, options?: KeyData): Promise<[ArrayBufferLike, KeyData]> { options ??= { name: 'PBKDF2', hash: Hashes.SHA_512, @@ -66,7 +65,7 @@ export class AES { return [new Uint8Array(bits), options] } - static async encrypt(data: TypedArray, key: TypedArray, mode: Modes = Modes.AES_GCM): Promise { + static async encrypt(data: ArrayBufferLike, key: ArrayBufferLike, mode: Modes = Modes.AES_GCM): Promise { const c = await getCrypto() let iv: Uint8Array @@ -88,7 +87,7 @@ export class AES { return AES.join(Bytes.encode(alg), iv, encryptedBuffer) } - static async decrypt(ciphertext: string, key: TypedArray): Promise { + static async decrypt(ciphertext: string, key: ArrayBufferLike): Promise { const c = await getCrypto() const [alg, iv, data] = await AES.split(ciphertext) @@ -107,7 +106,7 @@ export class AES { return new Uint8Array(decrypted) } - static async encryptEasy(data: string | TypedArray, key: string, mode: Modes = Modes.AES_GCM): Promise { + static async encryptEasy(data: string | ArrayBufferLike, key: string, mode: Modes = Modes.AES_GCM): Promise { const dataBuffer = typeof data === 'string' ? Bytes.encode(data) : data const [keyDerived, options] = await AES.derive(key) @@ -143,7 +142,7 @@ export class AES { return Bytes.decode(decrypted) } - static async generateKey(): Promise { + static async generateKey(): Promise { const c = await getCrypto() const key = await c.subtle.generateKey( { diff --git a/src/crypto/encoding.ts b/src/crypto/encoding.ts index bc9f77c..f353f5a 100644 --- a/src/crypto/encoding.ts +++ b/src/crypto/encoding.ts @@ -1,9 +1,9 @@ -import { split, type TypedArray } from '../utils/base.js' +import { split } from '../utils/base.js' export class Base64 { private static prefix = 'data:application/octet-stream;base64,' - static encode(s: TypedArray): Promise { + static encode(s: ArrayBufferLike): Promise { return split({ async node() { return Buffer.from(s).toString('base64') @@ -22,7 +22,7 @@ export class Base64 { }) } - static decode(s: string): Promise { + static decode(s: string): Promise { return split({ async node() { return Buffer.from(s, 'base64') @@ -38,7 +38,7 @@ export class Base64 { } export class Hex { - static encode(buffer: TypedArray): string { + static encode(buffer: ArrayBufferLike): string { let s = '' for (const i of new Uint8Array(buffer)) { s += i.toString(16).padStart(2, '0') @@ -46,7 +46,7 @@ export class Hex { return s } - static decode(s: string): TypedArray { + static decode(s: string): ArrayBufferLike { const size = s.length / 2 const buffer = new Uint8Array(size) for (let i = 0; i < size; i++) { @@ -59,7 +59,7 @@ export class Hex { } export class Bytes { - static decode(data: TypedArray): string { + static decode(data: ArrayBufferLike): string { return split({ node() { return Buffer.from(data).toString('utf-8') @@ -70,7 +70,7 @@ export class Bytes { }) } - static encode(data: string): TypedArray { + static encode(data: string): ArrayBufferLike { return split({ node() { return Buffer.from(data) diff --git a/src/crypto/hash.ts b/src/crypto/hash.ts index af21511..a4a7a71 100644 --- a/src/crypto/hash.ts +++ b/src/crypto/hash.ts @@ -1,4 +1,3 @@ -import { type TypedArray } from '../utils/base.js' import { getCrypto } from './crypto.js' import { Bytes, Hex } from './encoding.js' @@ -21,8 +20,8 @@ export enum Hashes { export class Hash { static async hash(data: string, hash: Hashes): Promise - static async hash(data: TypedArray, hash: Hashes): Promise - static async hash(data: string | TypedArray, hash: Hashes): Promise { + static async hash(data: ArrayBufferLike, hash: Hashes): Promise + static async hash(data: string | ArrayBufferLike, 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 3e73d89..9a5d5fd 100644 --- a/src/crypto/random.ts +++ b/src/crypto/random.ts @@ -1,7 +1,6 @@ -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/crypto/rsa.ts b/src/crypto/rsa.ts index d4e2f16..d006a6f 100644 --- a/src/crypto/rsa.ts +++ b/src/crypto/rsa.ts @@ -1,4 +1,3 @@ -import type { TypedArray } from '../utils/base.js' import { getCrypto } from './crypto.js' import { Base64 } from './encoding.js' @@ -71,7 +70,8 @@ class Key { // @ts-ignore const mod = key?.algorithm?.modulusLength if (isNaN(mod)) throw Constants.error.invalidKey - return mod / 8 - (2 * 512) / 8 - 2 + const maxBytes = mod / 8 - (2 * 512) / 8 - 2 + return maxBytes } } @@ -100,7 +100,7 @@ export class RSA { } } - static async encrypt(data: TypedArray, key: string): Promise { + static async encrypt(data: ArrayBufferLike, key: string): Promise { let keyObj: CryptoKey try { keyObj = await Key.decode(key) @@ -112,14 +112,14 @@ export class RSA { } // Check if data is too large - if (data.length > Key.getMaxMessageSize(keyObj)) throw Constants.error.dataTooLong + if (new Uint8Array(data).byteLength > Key.getMaxMessageSize(keyObj)) throw Constants.error.dataTooLong const c = await getCrypto() const encrypted = await c.subtle.encrypt({ name: Constants.name }, keyObj, data) return new Uint8Array(encrypted) } - static async decrypt(data: TypedArray, key: string): Promise { + static async decrypt(data: ArrayBufferLike, key: string): Promise { let keyObj: CryptoKey try { keyObj = await Key.decode(key) diff --git a/src/index.ts b/src/index.ts index e156607..24fa213 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,4 +3,3 @@ export * from './crypto/encoding.js' export * from './crypto/hash.js' export * from './crypto/random.js' export * from './crypto/rsa.js' -export { TypedArray } from './utils/base.js' diff --git a/src/utils/base.ts b/src/utils/base.ts index d52e9e8..3f92f93 100644 --- a/src/utils/base.ts +++ b/src/utils/base.ts @@ -8,17 +8,6 @@ export const isBrowser = typeof window !== 'undefined' */ export type PromiseOrValue = T | Promise -export type TypedArray = - | Int8Array - | Uint8Array - | Uint8ClampedArray - | Int16Array - | Uint16Array - | Int32Array - | Uint32Array - | BigInt64Array - | BigUint64Array - /** * @internal */