A Crypt Vault library for encryption and decryption of data using AES-256-CBC algorithm.
npm install crypt-vaultA TypeScript encryption package designed for both Node.js and browser environments, providing AES-256-CBC encryption, compression, and utilities like UUID generation and password hashing.
Please contact your admin to grant you the read access to this repo, then please run cmd:
bash
yarn add crypt-vault
`
For npm
`bash
npm install crypt-vault
`Setup
To initialize the encryption module, call
initializeEncryption with a secret key and optional initialization vector (IV):`typescript
import { initializeEncryption, encrypt, decrypt } from 'crypt-vault';// Initialize Encryption with secretKey and IV (optional)
initializeEncryption('your-secret-key', 'optional-iv');
`Functions Overview
$3
Initializes the encryption class with a 32-byte secretKey and an optional 16-byte iv.- Parameters:
-
secretKey (string): A 32-byte string used for encryption.
- iv (string, optional): A 16-byte initialization vector. If not provided, a random IV is generated.---
$3
Encrypts the input data using AES-256-CBC after compressing it.- Parameters:
-
data (any): The data to be encrypted. It can be an object, array, or string.
- Returns:
- string: The base64 encoded encrypted string.---
$3
Decrypts the input string (which must be in the format encrypted by encrypt) and decompresses it.- Parameters:
-
data (string): The base64 encoded string to be decrypted.
- Returns:
- any: The decrypted and decompressed data, parsed back to its original form (object, array, or string).---
$3
Encrypts the given data using a custom encryption key, instead of the one initialized globally.- Parameters:
-
data (any): The data to be encrypted.
- ENCRYPTION_KEY (string): A 32-byte string used as the encryption key.- Returns:
-
string | undefined: The base64 encoded encrypted string, or undefined if encryption fails.---
$3
Decrypts the input string using a custom encryption key.- Parameters:
-
data (string): The base64 encoded string to be decrypted.
- ENCRYPTION_KEY (string): A 32-byte string used as the decryption key.- Returns:
-
any: The decrypted and decompressed data, parsed back to its original form.---
$3
Compresses the input string using Gzip and converts it to a base64 string.- Parameters:
-
str (string): The string to be compressed.
- Returns:
- string: The base64 encoded compressed string.---
$3
Decompresses a base64 encoded compressed string (originally compressed by compressData).- Parameters:
-
str (string): The base64 encoded compressed string.
- Returns:
- string: The decompressed string.---
$3
Generates a version 4 UUID.- Returns:
-
string: A UUID v4 string (e.g., xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx).---
$3
Generates a SHA-256 hash of the input string and encodes it in base64.- Parameters:
-
data (string): The string to be hashed.
- Returns:
- string: The base64 encoded hash of the input.---
$3
Hashes the password with a randomly generated salt using SHA-256.- Parameters:
-
password (string): The password to be hashed.
- Returns:
- string: A hashed password with the salt, in the format salt$hash.---
$3
Compares a plaintext password with a hashed password.- Parameters:
-
password (string): The plaintext password.
- hashedPassword (string): The hashed password, in the format salt$hash.
- Returns:
- boolean: true if the password matches the hash, otherwise false.---
Example Usage
$3
`typescript
initializeEncryption('mysecretkey1234567890123456789012');// Encrypt data
const encryptedData = encrypt({ message: 'Hello, World!' });
console.log('Encrypted Data:', encryptedData);
// Decrypt data
const decryptedData = decrypt(encryptedData);
console.log('Decrypted Data:', decryptedData);
`$3
`typescript
const customKey = 'mycustomkey1234567890123456789012';
const encryptedData = dynamicEncrypt({ message: 'Hello, Custom!' }, customKey);
console.log('Dynamically Encrypted Data:', encryptedData);const decryptedData = dynamicDecrypt(encryptedData!, customKey);
console.log('Dynamically Decrypted Data:', decryptedData);
`$3
`typescript
const hashedPassword = hashPassword('mySecretPassword');
console.log('Hashed Password:', hashedPassword);const isMatch = comparePassword('mySecretPassword', hashedPassword);
console.log('Password Match:', isMatch);
`$3
`typescript
console.log('Generated UUID:', uuidv4());const hash = getHash('Hello, Hash!');
console.log('Generated Hash:', hash);
``---
MIT © Sarvadhi Solution