Crypto Helper
Cryptographic utilities for AES symmetric encryption, RSA asymmetric encryption, and hashing.
Quick Reference
| Class/Function | Algorithm | Use Case |
|---|---|---|
| AES | Symmetric | Fast encryption for data at rest (AES-256-CBC, AES-256-GCM) |
| RSA | Asymmetric | Key exchange, digital signatures (public/private keys) |
| hash() | MD5, SHA256 | Passwords, data integrity (HMAC support) |
AES Algorithms
| Algorithm | Mode | Features |
|---|---|---|
aes-256-cbc | CBC | Standard block cipher mode |
aes-256-gcm | GCM | Authenticated encryption |
Common Methods
| Operation | AES | RSA |
|---|---|---|
| Encrypt | aes.encrypt(message, secret) | rsa.encrypt(message, publicKey) |
| Decrypt | aes.decrypt(encrypted, secret) | rsa.decrypt(encrypted, privateKey) |
| Key Generation | N/A (use secret) | rsa.generateDERKeyPair() |
AES (Symmetric Encryption)
The AES class provides an interface for encrypting and decrypting data using the Advanced Encryption Standard.
Creating an AES Instance
You can create an AES instance by specifying the algorithm (aes-256-cbc or aes-256-gcm).
import { AES } from '@venizia/ignis';
const aes = AES.withAlgorithm('aes-256-cbc');Encrypting Data
The encrypt method takes a message and a secret key.
const secret = 'a-32-byte-long-secret-key-for-aes';
const message = 'This is a secret message.';
const encryptedMessage = aes.encrypt(message, secret);
// => Returns a base64 encoded string containing the IV and encrypted dataDecrypting Data
The decrypt method takes the encrypted message and the same secret key.
const decryptedMessage = aes.decrypt(encryptedMessage, secret);
// => 'This is a secret message.'RSA (Asymmetric Encryption)
The RSA class provides an interface for encrypting and decrypting data using the RSA algorithm.
Creating an RSA Instance
import { RSA } from '@venizia/ignis';
const rsa = RSA.withAlgorithm();Generating a Key Pair
The generateDERKeyPair method creates a new public/private key pair in DER format.
const { publicKey, privateKey } = rsa.generateDERKeyPair();Encrypting Data
Encrypt data using the public key.
const message = 'This is another secret.';
const encrypted = rsa.encrypt(message, publicKey.toString('base64'));Decrypting Data
Decrypt data using the private key.
const decrypted = rsa.decrypt(encrypted, privateKey.toString('base64'));
// => 'This is another secret.'Hash Utility
In addition to the Crypto helper, Ignis also provides a standalone hash utility function for creating hashes (e.g., for passwords or data integrity checks).
import { hash } from '@venizia/ignis';
// MD5 Hash
const md5Hash = hash('some text', { algorithm: 'MD5', outputType: 'hex' });
// SHA256 HMAC
const sha256Hash = hash('some text', {
algorithm: 'SHA256',
secret: 'a-secret-key',
outputType: 'hex',
});