Skip to content

Crypto Helper

Cryptographic utilities for AES symmetric encryption, RSA asymmetric encryption, and hashing.

Quick Reference

Class/FunctionAlgorithmUse Case
AESSymmetricFast encryption for data at rest (AES-256-CBC, AES-256-GCM)
RSAAsymmetricKey exchange, digital signatures (public/private keys)
hash()MD5, SHA256Passwords, data integrity (HMAC support)

AES Algorithms

AlgorithmModeFeatures
aes-256-cbcCBCStandard block cipher mode
aes-256-gcmGCMAuthenticated encryption

Common Methods

OperationAESRSA
Encryptaes.encrypt(message, secret)rsa.encrypt(message, publicKey)
Decryptaes.decrypt(encrypted, secret)rsa.decrypt(encrypted, privateKey)
Key GenerationN/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).

typescript
import { AES } from '@venizia/ignis';

const aes = AES.withAlgorithm('aes-256-cbc');

Encrypting Data

The encrypt method takes a message and a secret key.

typescript
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 data

Decrypting Data

The decrypt method takes the encrypted message and the same secret key.

typescript
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

typescript
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.

typescript
const { publicKey, privateKey } = rsa.generateDERKeyPair();

Encrypting Data

Encrypt data using the public key.

typescript
const message = 'This is another secret.';
const encrypted = rsa.encrypt(message, publicKey.toString('base64'));

Decrypting Data

Decrypt data using the private key.

typescript
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).

typescript
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',
});