cripta
Version:
A layer of encryption a little too judicious
261 lines (249 loc) • 9.07 kB
TypeScript
import { DataBag } from 't0n';
type AcceptedValue = null | boolean | string | number | bigint | Array<any> | object | Symbol | undefined;
declare const INDEX_TYPE: readonly ["null", "boolean", "string", "int", "float", "bigint", "array", "object", "symbol", "undefined"];
declare const TYPE: Record<typeof INDEX_TYPE[number], number>;
type ConfigOptions = {
key?: string;
entropy?: number;
seed?: number;
};
declare class Config {
#private;
entropy: number;
baseAlphabet: string;
key?: Buffer;
seed?: number;
alphabet?: string;
constructor(opts?: ConfigOptions);
setKey(key?: string): this;
setSeed(value: any): this;
setEntropy(value: any): this;
getInt(key: string): number;
sortSeed<T extends string | string[]>(data: T, seed?: number | string | null): T;
createSeededRNG(seed: number): () => number;
hashStringToNumber(str: string): number;
}
declare class Factory$1 {
#private;
private separator;
config: Config;
lastConfig: Config | null;
constructor(config?: ConfigOptions | Config);
same(): this;
unique(): this;
setConfig(config: ConfigOptions | Config): this;
onceConfig(config: ConfigOptions | Config): this;
onceKey(secret: string): this;
private maybeRestoreConfig;
private validateConfig;
encode(data: AcceptedValue): string;
decode(data: string): AcceptedValue;
private convert;
private toText;
private cipher;
private forgeKey;
private maybeUseAlphabet;
private strtr;
baseEncode(data: Buffer): string;
baseDecode(data: string): Buffer;
}
/**
* Defines the list of claims that are registered in the IANA "JSON Web Token Claims" registry
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1
*/
declare class Claim {
/**
* Identifies the recipients that the JWT is intended for
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1.3
*/
static readonly AUDIENCE: string;
/**
* Identifies the expiration time on or after which the JWT MUST NOT be accepted for processing
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1.4
*/
static readonly EXPIRATION_TIME: string;
/**
* Provides a unique identifier for the JWT
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1.7
*/
static readonly ID: string;
/**
* Identifies the time at which the JWT was issued
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1.6
*/
static readonly ISSUED_AT: string;
/**
* Identifies the principal that issued the JWT
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1.1
*/
static readonly ISSUER: string;
/**
* Identifies the time before which the JWT MUST NOT be accepted for processing
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1.5
*/
static readonly NOT_BEFORE: string;
/**
* Identifies the principal that is the subject of the JWT
*
* @see https://tools.ietf.org/html/rfc7519#section-4.1.2
*/
static readonly SUBJECT: string;
/**
* Identifies the time to verifies the claims iat, nbf, and exp,
* when present (supports leeway configuration)
*/
static readonly NOW: string;
static readonly BROWSER: string;
static readonly IP: string;
static readonly LOCATION: string;
static readonly ALL: string[];
static readonly RFC7519: string[];
static readonly DATE_CLAIMS: string[];
static readonly CUSTOM: string[];
}
declare class Token {
#private;
claims: DataBag;
header: DataBag;
headerString: string;
body: any;
bodyString: string;
signature: string;
secret(key: string): void;
parseHeader(header?: string | null | undefined): this;
encodedHeader(): string;
parseBody(body?: string | null | undefined): this;
encodedBody(): string;
setSignature(signature: string): this;
sign(): string;
signed(): boolean;
isValid(): boolean;
isInvalid(): boolean;
hasValue(prop: string, value?: any): boolean;
validateClaims(): boolean;
isExpired(date?: number | Date | null): boolean;
notBefore(date?: number | Date | null): boolean;
issuedBefore(date?: number | Date | null): boolean;
validate(claim: string, expected?: any): boolean;
protected getTokenValue(claim: string): any;
protected validateCustomClaims(): boolean;
same(entropy?: string | number): void;
unique(): void;
leeway(time?: number | Date | null): void;
getParts(token: string): string[] | false;
toString(): string;
}
declare class Creator {
protected token: Token;
same(): this;
unique(): this;
body(body: any): this;
payload(body: any): this;
content(body: any): this;
secret(key: string): this;
phrase(key: string): this;
passphrase(key: string): this;
expiresAt(date: number | Date): this;
expiration(date: number | Date): this;
exp(date: number | Date): this;
canOnlyBeUsedAfter(date: number | Date): this;
notBefore(date: number | Date): this;
nbf(date: number | Date): this;
issuedAt(date: number | Date): this;
issuedBefore(date: number | Date): this;
iat(date: number | Date): this;
identifiedBy(id: string): this;
jti(id: string): this;
relatedTo(subject: string): this;
sub(subject: string): this;
permittedFor(...aud: string[]): this;
audience(...aud: string[]): this;
aud(...aud: string[]): this;
issuedBy(issuer: string): this;
iss(issuer: string): this;
withClaim(claim: string, value: any): this;
with(claim: string, value: any): this;
get(): string;
toString(): string;
}
declare class Parser {
protected token: Token;
private tokenString;
private parsed;
constructor(token: string);
same(): this;
unique(): this;
leeway(time: number | Date): this;
secret(key: string): this;
phrase(key: string): this;
passphrase(key: string): this;
validAt(date: number | Date): this;
expiresAt(date: number | Date): this;
expiration(date: number | Date): this;
exp(date: number | Date): this;
canOnlyBeUsedAfter(date: number | Date): this;
notBefore(date: number | Date): this;
nbf(date: number | Date): this;
issuedAt(date: number | Date): this;
issuedBefore(date: number | Date): this;
iat(date: number | Date): this;
identifiedBy(id: string): this;
jti(id: string): this;
relatedTo(subject: string): this;
sub(subject: string): this;
permittedFor(...aud: string[]): this;
audience(...aud: string[]): this;
aud(...aud: string[]): this;
issuedBy(issuer: string): this;
iss(issuer: string): this;
withClaim(claim: string, value: any): this;
with(claim: string, value: any): this;
private parseToken;
isValid(): boolean;
isInvalid(): boolean;
hasValue(prop: string, value?: any): boolean;
has(prop: string, value?: any): boolean;
get<T = any>(): T;
toString(): string;
}
declare class Factory {
static create(): Creator;
static parse(token: string): Parser;
}
type BinaryToTextEncoding = 'base64' | 'base64url' | 'hex' | 'binary' | null;
type Encoding = BinaryToTextEncoding | 'buffer';
declare function md5(str: string): string;
declare function md5(str: Buffer): Buffer;
declare function md5(str: Buffer, encoding: 'buffer'): Buffer;
declare function md5(str: string, encoding: 'buffer' | null | undefined): Buffer;
declare function md5(str: string, encoding: Encoding): string;
declare function sha1(str: string): string;
declare function sha1(str: Buffer): Buffer;
declare function sha1(str: Buffer, encoding: 'buffer'): Buffer;
declare function sha1(str: string, encoding: 'buffer' | null | undefined): Buffer;
declare function sha1(str: string, encoding: Encoding): string;
declare function sha256(str: string): string;
declare function sha256(str: Buffer): Buffer;
declare function sha256(str: Buffer, encoding: 'buffer'): Buffer;
declare function sha256(str: string, encoding: 'buffer' | null | undefined): Buffer;
declare function sha256(str: string, encoding: Encoding): string;
declare function sha512(str: string): string;
declare function sha512(str: Buffer): Buffer;
declare function sha512(str: Buffer, encoding: 'buffer'): Buffer;
declare function sha512(str: string, encoding: 'buffer' | null | undefined): Buffer;
declare function sha512(str: string, encoding: Encoding): string;
declare const SALT_MAX_LEN = 16;
declare const SALT_MIN_LEN = 5;
declare const HASH_LEN = 40;
declare function hash(plain: string, salt?: string): string;
declare function compare(plain: string, encoded: string): boolean;
declare function getSalt(str: string): string;
declare const cripta: (config?: ConfigOptions | Config) => Factory$1;
export { type AcceptedValue, Claim, Config, type ConfigOptions, Factory$1 as Cripta, HASH_LEN, INDEX_TYPE, SALT_MAX_LEN, SALT_MIN_LEN, TYPE, Factory as Token, compare, cripta, getSalt, hash, md5, sha1, sha256, sha512 };