cryptix v1.0.3
Classes
AsimetricUtils
A utility class for asymmetric encryption using RSA. Provides functions for generating, storing, retrieving, encrypting, and decrypting data securely.
Kind: global class
- AsimetricUtils
- .generateRSAKeyPair(keySize) ⇒ Object
- .savePublicKey(publicKey, filePath)
- .loadPublicKey(filePath) ⇒ crypto.KeyObject
- .savePrivateKey(privateKey, filePath)
- .loadPrivateKey(filePath) ⇒ crypto.KeyObject
- .encryptWithPublicKey(data, publicKey) ⇒ Buffer
- .decryptWithPrivateKey(encryptedData, privateKey) ⇒ string
AsimetricUtils.generateRSAKeyPair(keySize) ⇒ Object
Generates an RSA key pair (public and private keys).
Kind: static method of AsimetricUtils
Returns: Object - An object containing the keys in PEM format.
Throws:
- Error If key generation fails.
Param | Type | Description |
---|---|---|
keySize | number | The size of the RSA key in bits (recommended: 2048 or 4096). |
Example
// Generate a 2048-bit RSA key pair
const keys = AsimetricUtils.generateRSAKeyPair(2048);
console.log("Public Key:\n", keys.publicKey);
console.log("Private Key:\n", keys.privateKey);
AsimetricUtils.savePublicKey(publicKey, filePath)
Saves a public key to a file.
Kind: static method of AsimetricUtils
Throws:
- Error If the file cannot be written.
Param | Type | Description |
---|---|---|
publicKey | string | The public key in PEM format. |
filePath | string | The file path where the key should be saved. |
Example
const keys = AsimetricUtils.generateRSAKeyPair(2048);
AsimetricUtils.savePublicKey(keys.publicKey, './public.pem');
console.log("Public key saved successfully!");
AsimetricUtils.loadPublicKey(filePath) ⇒ crypto.KeyObject
Loads a public key from a file.
Kind: static method of AsimetricUtils
Returns: crypto.KeyObject - The loaded public key.
Throws:
- Error If the file cannot be read.
Param | Type | Description |
---|---|---|
filePath | string | The path of the file containing the public key. |
Example
const publicKey = AsimetricUtils.loadPublicKey('./public.pem');
console.log("Loaded Public Key:\n", publicKey);
AsimetricUtils.savePrivateKey(privateKey, filePath)
Saves a private key to a file.
Kind: static method of AsimetricUtils
Throws:
- Error If the file cannot be written.
Param | Type | Description |
---|---|---|
privateKey | string | The private key in PEM format. |
filePath | string | The file path where the key should be saved. |
Example
const keys = AsimetricUtils.generateRSAKeyPair(2048);
AsimetricUtils.savePrivateKey(keys.privateKey, './private.pem');
console.log("Private key saved successfully!");
AsimetricUtils.loadPrivateKey(filePath) ⇒ crypto.KeyObject
Loads a private key from a file.
Kind: static method of AsimetricUtils
Returns: crypto.KeyObject - The loaded private key.
Throws:
- Error If the file cannot be read.
Param | Type | Description |
---|---|---|
filePath | string | The path of the file containing the private key. |
Example
const privateKey = AsimetricUtils.loadPrivateKey('./private.pem');
console.log("Loaded Private Key:\n", privateKey);
AsimetricUtils.encryptWithPublicKey(data, publicKey) ⇒ Buffer
Encrypts a message using a public key.
Kind: static method of AsimetricUtils
Returns: Buffer - The encrypted message.
Throws:
- Error If encryption fails.
Param | Type | Description |
---|---|---|
data | string | The plaintext message to encrypt. |
publicKey | crypto.KeyObject | The public key used for encryption. |
Example
const publicKey = AsimetricUtils.loadPublicKey('./public.pem');
const encrypted = AsimetricUtils.encryptWithPublicKey("Hello, World!", publicKey);
console.log("Encrypted Data:", encrypted.toString('base64'));
AsimetricUtils.decryptWithPrivateKey(encryptedData, privateKey) ⇒ string
Decrypts a message using a private key.
Kind: static method of AsimetricUtils
Returns: string - The decrypted message.
Throws:
- Error If decryption fails.
Param | Type | Description |
---|---|---|
encryptedData | Buffer | The encrypted data. |
privateKey | crypto.KeyObject | The private key used for decryption. |
Example
const privateKey = AsimetricUtils.loadPrivateKey('./private.pem');
const decrypted = AsimetricUtils.decryptWithPrivateKey(encryptedData, privateKey);
console.log("Decrypted Message:", decrypted);
Decryptor
Utility class for decrypting AES-GCM encrypted data using a password-derived key.
Kind: global class
Decryptor.ITERATIONS
Number of PBKDF2 iterations for key derivation (higher = more secure but slower).
Kind: static property of Decryptor
Decryptor.SALT_LENGTH
Salt length in bytes (16 bytes = 128 bits, recommended for security).
Kind: static property of Decryptor
Decryptor.KEY_LENGTH
Key length in bytes (32 bytes = 256 bits, AES-256).
Kind: static property of Decryptor
Decryptor.IV_LENGTH
Initialization Vector (IV) length in bytes (12 bytes is recommended for AES-GCM).
Kind: static property of Decryptor
Decryptor.AUTH_TAG_LENGTH
Authentication Tag length in bytes (16 bytes ensures message integrity).
Kind: static property of Decryptor
Decryptor.decrypt(encryptedText, password) ⇒ string | undefined
Decrypts an AES-GCM encrypted message using a password.
Kind: static method of Decryptor
Returns: string | undefined - - The decrypted plaintext string, or undefined
if decryption fails.
Throws:
- Error If decryption fails.
Param | Type | Description |
---|---|---|
encryptedText | string | The Base64-encoded encrypted string (contains salt + IV + cipherText + authTag). |
password | string | The password used to derive the decryption key. |
Example
const decrypted = Decryptor.decrypt(encryptedData, "my_secure_password");
console.log("Decrypted Text:", decrypted);
EncryptFile
Utility class for encrypting files using a combination of random per-file keys and a fixed secret key.
Kind: global class
EncryptFile.encryptFile(inputDecPath, outputEncPath)
Encrypts a plaintext file and saves it to a new location with obfuscation techniques applied.
Kind: static method of EncryptFile
Throws:
- Error If file operations fail or encryption encounters an error.
Param | Type | Description |
---|---|---|
inputDecPath | string | Path to the plaintext input file. |
outputEncPath | string | Path where the encrypted file will be saved. |
Example
EncryptFile.encryptFile("data.txt", "data.enc");
Encryptor
Provides AES-GCM encryption with PBKDF2 key derivation for strong security.
Kind: global class
Encryptor.ITERATIONS
Number of iterations for PBKDF2 (increases brute-force resistance). Higher values = more security but slower processing.
Kind: static property of Encryptor
Encryptor.SALT_LENGTH
Length of the salt in bytes (16 bytes = 128 bits). Salt ensures each encryption is unique, even with the same password.
Kind: static property of Encryptor
Encryptor.KEY_LENGTH
AES-256 key length in bytes (32 bytes = 256 bits). AES-256 requires a 32-byte key for maximum security.
Kind: static property of Encryptor
Encryptor.IV_LENGTH
IV (Initialization Vector) length in bytes (12 bytes = recommended for GCM mode). IV ensures non-repeating ciphertext for the same input.
Kind: static property of Encryptor
Encryptor.AUTH_TAG_LENGTH
Authentication Tag length in bytes (16 bytes = 128 bits). Ensures the integrity of the ciphertext and prevents tampering.
Kind: static property of Encryptor
Encryptor.encrypt(plainText, password) ⇒ string
Encrypts a plaintext string using AES-256-GCM with a password-derived key.
Kind: static method of Encryptor
Returns: string - - The encrypted text, encoded in Base64 (salt + IV + cipherText + authTag).
Throws:
- Error - If encryption fails.
Param | Type | Description |
---|---|---|
plainText | string | The text to be encrypted. |
password | string | The password used to derive the encryption key. |
Example
const encrypted = Encryptor.encrypt("Hello, world!", "my_secure_password");
console.log(encrypted); // Encrypted text in Base64 format
HashingUtils
Provides methods for securely hashing passwords using PBKDF2, salt, and a pepper.
Kind: global class
- HashingUtils
- .generateSecureHashSHA512(input) ⇒ string
- .generateSecureHashSHA256(input) ⇒ string
- .verifyHashSHA512(input, storedHash) ⇒ boolean
- .verifyHashSHA256(input, storedHash) ⇒ boolean
- .generateSecureHash(input, algorithm) ⇒ string
- .verifyHash(input, storedHash, algorithm) ⇒ boolean
- .deriveKey(input, salt, algorithm) ⇒ Buffer
- .generateSalt() ⇒ Buffer
HashingUtils.generateSecureHashSHA512(input) ⇒ string
Generates a secure SHA-512 hash using PBKDF2, a random salt, and a secret pepper.
Kind: static method of HashingUtils
Returns: string - The Base64 encoded hash (salt$hash
format).
Param | Type | Description |
---|---|---|
input | string | The plain text to hash. |
HashingUtils.generateSecureHashSHA256(input) ⇒ string
Generates a secure SHA-256 hash using PBKDF2, a random salt, and a secret pepper.
Kind: static method of HashingUtils
Returns: string - The Base64 encoded hash (salt$hash
format).
Param | Type | Description |
---|---|---|
input | string | The plain text to hash. |
HashingUtils.verifyHashSHA512(input, storedHash) ⇒ boolean
Verifies if a SHA-512 hash matches the plain text.
Kind: static method of HashingUtils
Returns: boolean - true
if the hash matches, false
otherwise.
Param | Type | Description |
---|---|---|
input | string | The plain text to check. |
storedHash | string | The stored hash (salt$hash in Base64). |
HashingUtils.verifyHashSHA256(input, storedHash) ⇒ boolean
Verifies if a SHA-256 hash matches the plain text.
Kind: static method of HashingUtils
Returns: boolean - true
if the hash matches, false
otherwise.
Param | Type | Description |
---|---|---|
input | string | The plain text to check. |
storedHash | string | The stored hash (salt$hash in Base64). |
HashingUtils.generateSecureHash(input, algorithm) ⇒ string
Generates a secure hash using PBKDF2 + Salt + Pepper.
Kind: static method of HashingUtils
Returns: string - The Base64 encoded hash (salt$hash
format).
Param | Type | Description |
---|---|---|
input | string | The plain text to hash. |
algorithm | "sha512" | "sha256" | The hashing algorithm to use. |
HashingUtils.verifyHash(input, storedHash, algorithm) ⇒ boolean
Verifies if a hash corresponds to the plain text.
Kind: static method of HashingUtils
Returns: boolean - true
if the hash matches, false
otherwise.
Param | Type | Description |
---|---|---|
input | string | The plain text to check. |
storedHash | string | The stored hash (salt$hash in Base64). |
algorithm | "sha512" | "sha256" | The hashing algorithm used. |
HashingUtils.deriveKey(input, salt, algorithm) ⇒ Buffer
Derives a key using PBKDF2 + Pepper.
Kind: static method of HashingUtils
Returns: Buffer - The derived hash.
Param | Type | Description |
---|---|---|
input | string | The plain text. |
salt | Buffer | The salt used in hashing. |
algorithm | "sha512" | "sha256" | The hashing algorithm used. |
HashingUtils.generateSalt() ⇒ Buffer
Generates a random salt.
Kind: static method of HashingUtils
Returns: Buffer - The random salt.
KeyGenerator
Class for generating secure keys and random passwords using strong cryptography.
Kind: global class
- KeyGenerator
- .generateKey(password, salt) ⇒ Promise.<Buffer>
- .generateSalt() ⇒ Buffer
- .generateSecurePassword(length) ⇒ string
KeyGenerator.generateKey(password, salt) ⇒ Promise.<Buffer>
Generates a secure AES key from a password and salt using PBKDF2 with HMAC-SHA-512.
Kind: static method of KeyGenerator
Returns: Promise.<Buffer> - A secure 256-bit (32-byte) derived key.
Throws:
- Error If an error occurs during the key generation.
Param | Type | Description |
---|---|---|
password | string | The password used to generate the key. |
salt | Buffer | The random salt used in the key derivation. |
KeyGenerator.generateSalt() ⇒ Buffer
Generates a random salt of fixed length.
Kind: static method of KeyGenerator
Returns: Buffer - A random 16-byte salt buffer.
KeyGenerator.generateSecurePassword(length) ⇒ string
Generates a secure random password containing alphanumeric characters and special symbols.
Kind: static method of KeyGenerator
Returns: string - A securely generated random password.
Throws:
- Error If the length is less than or equal to 0.
Param | Type | Description |
---|---|---|
length | number | The length of the generated password (minimum 1). |
SecureKeys
Class for managing secure key storage, decryption, and retrieval. This class supports loading encrypted data, decrypting it, and storing it in memory for fast access.
Kind: global class
- SecureKeys
- .init(encFilePath) ⇒ Promise.<void>
- .parseDecryptedContent()
- .decrypt(encryptedText, password) ⇒ string | undefined
- .getKey(keyName) ⇒ any
- .extractEncryptedKey(content) ⇒ string | null
- .extractEncryptedData(content) ⇒ string | null
SecureKeys.init(encFilePath) ⇒ Promise.<void>
Initializes the class by loading and decrypting the content of an encrypted file.
Kind: static method of SecureKeys
Returns: Promise.<void> - Resolves when the file is loaded and decrypted successfully.
Throws:
- Error If an error occurs during file reading or decryption.
Param | Type | Description |
---|---|---|
encFilePath | string | The path to the encrypted .enc file. |
SecureKeys.parseDecryptedContent()
Parses the decrypted content and stores it in a key-value map. Only processes lines that match the format: "| key -> value".
Kind: static method of SecureKeys
SecureKeys.decrypt(encryptedText, password) ⇒ string | undefined
Decrypts a given encrypted text using AES-GCM with a key derived from PBKDF2.
Kind: static method of SecureKeys
Returns: string | undefined - The decrypted text or undefined if an error occurs.
Throws:
- Error If decryption fails.
Param | Type | Description |
---|---|---|
encryptedText | string | The encrypted text in Base64. |
password | string | The password used to derive the decryption key. |
SecureKeys.getKey(keyName) ⇒ any
Retrieves the value associated with a key from the in-memory key-value map.
Kind: static method of SecureKeys
Returns: any - The associated value, or null if the key does not exist.
Param | Type | Description |
---|---|---|
keyName | string | The name of the key to retrieve. |
SecureKeys.extractEncryptedKey(content) ⇒ string | null
Extracts the encrypted key from the encrypted file content.
Kind: static method of SecureKeys
Returns: string | null - The encrypted key in Base64 format, or null if not found.
Param | Type | Description |
---|---|---|
content | string | The content of the encrypted file. |
SecureKeys.extractEncryptedData(content) ⇒ string | null
Extracts the encrypted data from the encrypted file content.
Kind: static method of SecureKeys
Returns: string | null - The encrypted data in Base64 format, or null if not found.
Param | Type | Description |
---|---|---|
content | string | The content of the encrypted file. |
SecurityUtils
This class provides advanced security utilities, including HMAC generation and verification, secure key generation using PBKDF2, random key and salt generation, and more.
Kind: global class
- SecurityUtils
- .generateHMAC(message, secretKey) ⇒ string | undefined
- .verifyHMAC(message, secretKey, receivedHMAC) ⇒ boolean
- .generateSecureKey(password, salt) ⇒ string | undefined
- .generateRandomKey() ⇒ string
- .generateSalt() ⇒ Buffer
SecurityUtils.generateHMAC(message, secretKey) ⇒ string | undefined
Generates an HMAC (Hashed Message Authentication Code) using SHA-512 with a secret key.
Kind: static method of SecurityUtils
Returns: string | undefined - The generated HMAC in Base64 format, or undefined if an error occurs.
Throws:
- Error If there is an error during HMAC generation.
Param | Type | Description |
---|---|---|
message | string | The message to authenticate. |
secretKey | string | The secret key used to generate the HMAC. |
SecurityUtils.verifyHMAC(message, secretKey, receivedHMAC) ⇒ boolean
Verifies if a given HMAC is valid by comparing it with the computed HMAC for the message.
Kind: static method of SecurityUtils
Returns: boolean - Returns true if the HMAC is valid, false otherwise.
Param | Type | Description |
---|---|---|
message | string | The original message. |
secretKey | string | The secret key used to generate the HMAC. |
receivedHMAC | string | The received HMAC to verify. |
SecurityUtils.generateSecureKey(password, salt) ⇒ string | undefined
Generates a secure secret key using PBKDF2 with HMAC-SHA-512.
Kind: static method of SecurityUtils
Returns: string | undefined - A secure secret key in Base64 format, or undefined if an error occurs.
Throws:
- Error If there is an error during key generation.
Param | Type | Description |
---|---|---|
password | string | The base password to derive the key. |
salt | Buffer | A random value to strengthen the key. |
SecurityUtils.generateRandomKey() ⇒ string
Generates a secure random secret key.
Kind: static method of SecurityUtils
Returns: string - A random secret key in Base64 format.
SecurityUtils.generateSalt() ⇒ Buffer
Generates a secure random salt.
Kind: static method of SecurityUtils
Returns: Buffer - A random salt in bytes (256 bits).