UNPKG

symmetricmorph

Version:

High-performance symmetric stream cipher with dynamic masking, cascading feedback, built-in MAC, and chunked encryption support. Lightweight and dependency-free.

141 lines (140 loc) 5.75 kB
/** * SymmetricMorph * * Unique stream symmetric encryption algorithm with cascading feedback, dynamic masking, * built-in protection against known plaintext attacks, stream MAC and resistance to side attacks. * Developed without the use of third-party libraries. */ export default class SymmetricMorph { private readonly key; private readonly salt?; private constructor(); /** * @returns Returns the salt used for key derivation, if available. */ getSalt(): Uint8Array | undefined; /** * Creates a `SymmetricMorph` instance from a password. * @param password - The password to derive the encryption key. * @param iterations - Number of iterations for key derivation (default: 20000). * @param keyLength - Length of the derived key in bytes (default: 64). * @returns A new `SymmetricMorph` instance. */ static fromPassword(password: string, iterations?: number, keyLength?: number): SymmetricMorph; /** * Creates a `SymmetricMorph` instance from a password and salt. * @param password - The password to derive the encryption key. * @param salt - The salt as an array of bytes or a number array. * @param iterations - Number of iterations for key derivation (default: 20000). * @param keyLength - Length of the derived key in bytes (default: 64). * @returns A new `SymmetricMorph` instance. */ static fromPasswordWithSalt(password: string, salt: Uint8Array | number[], iterations?: number, keyLength?: number): SymmetricMorph; /** * Creates a `SymmetricMorph` instance from a raw key. * @param key - The encryption key as an array of bytes. * @returns A new `SymmetricMorph` instance. */ static fromKey(key: Uint8Array): SymmetricMorph; /** * Generates a random encryption key of the specified length. * @param length - Length of the key in bytes (default: 64). * @returns A randomly generated key as an array of bytes. */ static generateKey(length?: number): Uint8Array; /** * Encrypts an array of plaintext bytes. * @param plainBytes - The plaintext data as an array of bytes. * @returns The encrypted data as an array of bytes. */ encrypt(plainBytes: Uint8Array): Uint8Array; /** * Decrypts an array of encrypted bytes. * @param encryptedBytes - The encrypted data as an array of bytes. * @returns The decrypted plaintext data as an array of bytes. * @throws Error if MAC verification fails. */ decrypt(encryptedBytes: Uint8Array): Uint8Array; /** * Encrypts multiple chunks of plaintext data. * @param chunks - An array of plaintext byte arrays. * @returns An array of encrypted byte arrays. */ encryptChunks(chunks: Uint8Array[]): Uint8Array[]; /** * Decrypts multiple chunks of encrypted data. * @param chunks - An array of encrypted byte arrays. * @returns An array of decrypted plaintext byte arrays. */ decryptChunks(chunks: Uint8Array[]): Uint8Array[]; /** * Updates the internal state during encryption or decryption. * @param state - The current state array. * @param rotated - The rotated byte value. * @param feedback - The feedback byte. * @param r - The random byte from the PRNG. * @param iteration - The current iteration index. * @param prev1 - The previous byte value (1 step back). * @param prev2 - The previous byte value (2 steps back). * @param prev3 - The previous byte value (3 steps back). */ private updateState; /** * Initializes the internal state array based on the encryption key. * @returns The initialized state array. */ private initState; /** * Creates a pseudo-random number generator (PRNG) based on a seed. * @param seed - The seed array for the PRNG. * @returns A function that generates random bytes. */ private static createPrng; /** * Derives a cryptographic key from a password and salt using iterative hashing. * @param pass - The password as an array of bytes. * @param salt - The salt as an array of bytes. * @param rounds - The number of hashing iterations. * @param length - The desired key length in bytes. * @returns The derived key as an array of bytes. */ private static deriveKey; /** * Generates a Message Authentication Code (MAC) for data integrity verification. * @param data - The data to generate the MAC for. * @param key - The encryption key. * @returns The MAC as an array of bytes. */ private static mac; /** * Generates a MAC based on accumulated MAC state and the encryption key. * @param macAcc - The accumulated MAC state. * @param key - The encryption key. * @returns The generated MAC as an array of bytes. */ private static generateMac; /** * Generates a random salt for key derivation. * @param length - The length of the salt in bytes. * @returns The generated salt as an array of bytes. */ private static generateSalt; /** * Generates a random nonce for encryption. * @returns The generated nonce as an array of bytes. */ private static generateNonce; /** * Converts a string to an array of bytes. * @param str - The input string. * @returns The string as an array of bytes. */ private static strToBytes; /** * Compares two byte arrays in constant time to prevent timing attacks. * @param a - The first byte array. * @param b - The second byte array. * @returns `true` if the arrays are equal, otherwise `false`. */ private static constantTimeCompare; }