UNPKG

@li0ard/gost3413

Version:

Cipher modes and padding's according to GOST R 34.13-2015 in pure TypeScript

71 lines (70 loc) 4.67 kB
/** * Bytes API type helpers for old + new TypeScript. * * TS 5.6 has `Uint8Array`, while TS 5.9+ made it generic `Uint8Array<ArrayBuffer>`. * We can't use specific return type, because TS 5.6 will error. * We can't use generic return type, because most TS 5.9 software will expect specific type. * * Maps typed-array input leaves to broad forms. * These are compatibility adapters, not ownership guarantees. * * - `TArg` keeps byte inputs broad. * - `TRet` marks byte outputs for TS 5.6 and TS 5.9+ compatibility. */ export type TypedArg<T> = T extends BigInt64Array ? BigInt64Array : T extends BigUint64Array ? BigUint64Array : T extends Float32Array ? Float32Array : T extends Float64Array ? Float64Array : T extends Int16Array ? Int16Array : T extends Int32Array ? Int32Array : T extends Int8Array ? Int8Array : T extends Uint16Array ? Uint16Array : T extends Uint32Array ? Uint32Array : T extends Uint8ClampedArray ? Uint8ClampedArray : T extends Uint8Array ? Uint8Array : never; /** Maps typed-array output leaves to narrow TS-compatible forms. */ export type TypedRet<T> = T extends BigInt64Array ? ReturnType<typeof BigInt64Array.of> : T extends BigUint64Array ? ReturnType<typeof BigUint64Array.of> : T extends Float32Array ? ReturnType<typeof Float32Array.of> : T extends Float64Array ? ReturnType<typeof Float64Array.of> : T extends Int16Array ? ReturnType<typeof Int16Array.of> : T extends Int32Array ? ReturnType<typeof Int32Array.of> : T extends Int8Array ? ReturnType<typeof Int8Array.of> : T extends Uint16Array ? ReturnType<typeof Uint16Array.of> : T extends Uint32Array ? ReturnType<typeof Uint32Array.of> : T extends Uint8ClampedArray ? ReturnType<typeof Uint8ClampedArray.of> : T extends Uint8Array ? ReturnType<typeof Uint8Array.of> : never; /** Recursively adapts byte-carrying API input types. See {@link TypedArg}. */ export type TArg<T> = T | ([TypedArg<T>] extends [never] ? T extends (...args: infer A) => infer R ? ((...args: { [K in keyof A]: TRet<A[K]>; }) => TArg<R>) & { [K in keyof T]: T[K] extends (...args: any) => any ? T[K] : TArg<T[K]>; } : T extends [infer A, ...infer R] ? [TArg<A>, ...{ [K in keyof R]: TArg<R[K]>; }] : T extends readonly [infer A, ...infer R] ? readonly [TArg<A>, ...{ [K in keyof R]: TArg<R[K]>; }] : T extends (infer A)[] ? TArg<A>[] : T extends readonly (infer A)[] ? readonly TArg<A>[] : T extends Promise<infer A> ? Promise<TArg<A>> : T extends object ? { [K in keyof T]: TArg<T[K]>; } : T : TypedArg<T>); /** Recursively adapts byte-carrying API output types. See {@link TypedArg}. */ export type TRet<T> = T extends unknown ? T & ([TypedRet<T>] extends [never] ? T extends (...args: infer A) => infer R ? ((...args: { [K in keyof A]: TArg<A[K]>; }) => TRet<R>) & { [K in keyof T]: T[K] extends (...args: any) => any ? T[K] : TRet<T[K]>; } : T extends [infer A, ...infer R] ? [TRet<A>, ...{ [K in keyof R]: TRet<R[K]>; }] : T extends readonly [infer A, ...infer R] ? readonly [TRet<A>, ...{ [K in keyof R]: TRet<R[K]>; }] : T extends (infer A)[] ? TRet<A>[] : T extends readonly (infer A)[] ? readonly TRet<A>[] : T extends Promise<infer A> ? Promise<TRet<A>> : T extends object ? { [K in keyof T]: TRet<T[K]>; } : T : TypedRet<T>) : never; /** Type for cipher function */ export type CipherFunc = (data: TArg<Uint8Array>) => TRet<Uint8Array>; /** Key size */ export declare const KEYSIZE = 32; /** ACPKM class */ export interface ACPKMClass { /** Encrypting function, that takes block as input */ encrypt(block: TArg<Uint8Array>): TRet<Uint8Array>; } /** ACPKM class constructor */ export interface ACPKMConstructor { new (key: TArg<Uint8Array>): ACPKMClass; } /** ACPKM Parameters */ export interface ACPKMParameters { /** ACPKM cipher class */ cipherClass: ACPKMConstructor; /** ACPKM section size (N) */ sectionSize: number; } export declare const xor: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>; export declare function equalBytes(a: TArg<Uint8Array>, b: TArg<Uint8Array>): boolean; export declare function concatBytes(...arrays: Uint8Array[]): TRet<Uint8Array>; export declare function hexToBytes(hex: string): TRet<Uint8Array>; export declare const bytesToHex: (bytes: TArg<Uint8Array>) => string; export declare const numberToBytesBE: (n: number | bigint, len: number) => TRet<Uint8Array>; export declare const numberToBytesLE: (n: number | bigint, len: number) => TRet<Uint8Array>; export declare const hexToNumber: (hex: string) => bigint; export declare const bytesToNumberBE: (bytes: TArg<Uint8Array>) => bigint; export declare const bytesToNumberLE: (bytes: TArg<Uint8Array>) => bigint;