UNPKG

ff1-js

Version:

FF1 (Format-Preserving Encryption) implementation in JavaScript/TypeScript

113 lines 3.7 kB
/** * FF1 (Format-Preserving Encryption) implementation * Based on NIST SP 800-38G specification */ export declare class FF1 { private key; private tweak; private radix; private alphabet; private minLength; private maxLength; /** * Creates a new FF1 instance * @param key - The encryption key (must be 16, 24, or 32 bytes) * @param tweak - The tweak value (must be between 0 and 2^104 bytes) * @param radix - The radix (base) of the alphabet (2-256) * @param alphabet - The alphabet string (must have length equal to radix) * @param minLength - Minimum input length (default: 2) * @param maxLength - Maximum input length (default: 100) */ constructor(key: Buffer | string, tweak: Buffer | string, radix: number, alphabet: string, minLength?: number, maxLength?: number); /** * Validates the constructor parameters */ private validateParameters; /** * Converts a string to a bigint using the specified radix and alphabet */ private number; /** * Converts a bigint to a string using the specified radix and alphabet */ private str; /** * Pseudo-random function (PRF) using HMAC-SHA256 */ private prf; /** * AES encryption in ECB mode */ private ciph; /** * XOR operation between two buffers */ private xor; /** * Converts bigint to bytes */ private bigIntToBytes; /** * Converts bytes to bigint */ private bytesToBigInt; /** * Main FF1 cipher function */ private cipher; /** * Encrypts a string using FF1 * @param input - The string to encrypt * @param tweak - Optional tweak value (uses default if not provided) * @returns The encrypted string */ encrypt(input: string, tweak?: Buffer | string): string; /** * Decrypts a string using FF1 * @param input - The string to decrypt * @param tweak - Optional tweak value (uses default if not provided) * @returns The decrypted string */ decrypt(input: string, tweak?: Buffer | string): string; /** * Gets the current configuration */ getConfig(): { radix: number; alphabet: string; minLength: number; maxLength: number; keyLength: number; tweakLength: number; }; } /** * Predefined alphabets for common use cases */ export declare const Alphabets: { /** Numeric alphabet (0-9) */ readonly NUMERIC: "0123456789"; /** Lowercase alphabet (a-z) */ readonly LOWERCASE: "abcdefghijklmnopqrstuvwxyz"; /** Uppercase alphabet (A-Z) */ readonly UPPERCASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** Alphanumeric alphabet (0-9, a-z, A-Z) */ readonly ALPHANUMERIC: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** Hexadecimal alphabet (0-9, a-f) */ readonly HEXADECIMAL: "0123456789abcdef"; /** Base64 alphabet */ readonly BASE64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; }; /** * Creates an FF1 instance with common configurations */ export declare function createFF1(key: Buffer | string, tweak: Buffer | string, alphabet?: string, minLength?: number, maxLength?: number): FF1; /** * Creates an FF1 instance for numeric encryption */ export declare function createNumericFF1(key: Buffer | string, tweak: Buffer | string, minLength?: number, maxLength?: number): FF1; /** * Creates an FF1 instance for alphanumeric encryption */ export declare function createAlphanumericFF1(key: Buffer | string, tweak: Buffer | string, minLength?: number, maxLength?: number): FF1; //# sourceMappingURL=ff1.d.ts.map