crypt-vault
v0.1.2
Published
A Crypt Vault library for encryption and decryption of data using AES-256-CBC algorithm.
Downloads
29
Maintainers
Readme
Encryption Package
A 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.
Installation
Please contact your admin to grant you the read access to this repo, then please run cmd:
For Yarn
yarn add crypt-vault
For npm
npm install crypt-vault
Setup
To initialize the encryption module, call initializeEncryption
with a secret key and optional initialization vector (IV):
import { initializeEncryption, encrypt, decrypt } from 'crypt-vault';
// Initialize Encryption with secretKey and IV (optional)
initializeEncryption('your-secret-key', 'optional-iv');
Functions Overview
1. initializeEncryption(secretKey: string, iv?: string): void
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.
2. encrypt(data: any): string
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. decrypt(data: string): any
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).
4. dynamicEncrypt(data: any, ENCRYPTION_KEY: string): string | undefined
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, orundefined
if encryption fails.
5. dynamicDecrypt(data: string, ENCRYPTION_KEY: string): any
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.
6. compressData(str: string): string
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.
7. decompressData(str: string): string
Decompresses a base64 encoded compressed string (originally compressed by compressData
).
- Parameters:
str
(string): The base64 encoded compressed string.
- Returns:
string
: The decompressed string.
8. uuidv4(): string
Generates a version 4 UUID.
- Returns:
string
: A UUID v4 string (e.g.,xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
).
9. getHash(data: string): string
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.
10. hashPassword(password: string): string
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 formatsalt$hash
.
11. comparePassword(password: string, hashedPassword: string): boolean
Compares a plaintext password with a hashed password.
- Parameters:
password
(string): The plaintext password.hashedPassword
(string): The hashed password, in the formatsalt$hash
.
- Returns:
boolean
:true
if the password matches the hash, otherwisefalse
.
Example Usage
Encrypting and Decrypting Data:
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);
Dynamic Encryption and Decryption:
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);
Password Hashing and Comparison:
const hashedPassword = hashPassword('mySecretPassword');
console.log('Hashed Password:', hashedPassword);
const isMatch = comparePassword('mySecretPassword', hashedPassword);
console.log('Password Match:', isMatch);
Generating UUID and SHA256 Hash:
console.log('Generated UUID:', uuidv4());
const hash = getHash('Hello, Hash!');
console.log('Generated Hash:', hash);
License
MIT © Sarvadhi Solution