react-native-crypto-vault is a secure, cross-platform library for managing cryptographic keys, encrypting/decrypting sensitive data, and handling vault-based key policies in React Native.
It is designed to provide developer-friendly APIs while enforcing best security practices.
Mobile applications often need to store highly sensitive data, including:
- Authentication tokens and API keys
- Payment or banking credentials
- User passwords, PINs, or personal secrets
Existing solutions are often:
- Exposing keys in memory or logs
- Requiring complex, unsafe handling of keys
- Platform-specific with no unified API
We built react-native-crypto-vault to provide a secure, cross-platform, and developer-friendly interface for handling cryptographic operations in React Native.
| Algorithm | Purpose |
|---|---|
| AES-GCM (256-bit) | Fast symmetric encryption with authenticated encryption, protecting confidentiality & integrity |
| HMAC-SHA256 | Ensures message integrity and authenticity when combined with AES-GCM or used independently |
| SHA-256 | Secure one-way hash for passwords, PINs, or sensitive string data |
| SecureRandom | Cryptographically secure generation of salts, IVs, and random keys |
- Keys are never exported in plaintext
- AES-GCM uses random IVs per encryption
- HMAC ensures authenticated encryption (detects tampering)
- Each key has a unique alias, preventing accidental overwrites
- Vault supports PIN or biometric unlocking (future enhancements)
- Generate and store AES keys securely in Android Keystore / iOS Keychain
- Retrieve stored keys via a unique alias
- Automatic key generation if key does not exist
- Future support for backup and restore
- AES-GCM encryption using 256-bit keys
- AES-GCM decryption returns original plaintext securely
- Optional combination with HMAC for authenticated encryption
- SHA-256 hashing for strings or messages
- HMAC-SHA256 signing using secure keys for message integrity
- Generate cryptographically secure random bytes
- Generate UUIDs for unique identifiers
- Lock/unlock vault with PIN, biometric, or none
- Auto-lock after inactivity
- Backup & restore vault securely
npm install react-native-crypto-vault
# or
yarn add react-native-crypto-vault** Android
Uses Android Keystore automatically
No extra configuration required
iOS
Uses iOS Keychain
Enable Keychain entitlements if necessary **
import CryptoVault from 'react-native-crypto-vault';
const alias = 'my_app_aes_key';
// Generates key if not exists, returns key alias
await CryptoVault.generateSecureKey(alias);
console.log('Secure key generated:', alias);
const plainText = 'Hello Secret';
// Encrypt
const cipherText = await CryptoVault.aesGcmEncrypt(plainText, alias);
console.log('Encrypted:', cipherText);
// Decrypt
const decrypted = await CryptoVault.aesGcmDecrypt(cipherText, alias);
console.log('Decrypted:', decrypted); // "Hello Secret"
Security Note: AES-GCM uses random IV per encryption to prevent repeated patterns.
const hash = await CryptoVault.hashString('my secret');
console.log('SHA-256 Hash:', hash);
Use Case: Storing hashed passwords or verifying message integrity.
const message = 'message-to-sign';
const hmac = await CryptoVault.hmacSHA256(message, alias);
console.log('HMAC:', hmac);
Security Purpose: Authenticate messages and prevent tampering.
const randomKey = await CryptoVault.getRandomBytes(32);
const encrypted = await CryptoVault.aesGcmEncryptWithHmac('secure-message', randomKey);
const decryptedMessage = await CryptoVault.aesGcmDecryptWithHmac(encrypted, randomKey);
console.log('Decrypted Message:', decryptedMessage); // "secure-message"
Benefit: Ensures both confidentiality and integrity in one operation.
// Get unique device ID
const deviceId = await CryptoVault.getDeviceInfo();
console.log('Device ID:', deviceId);
// Generate random UUID
const uuid = await CryptoVault.getRandomId();
console.log('Random UUID:', uuid);
//Use AES-GCM (with optional HMAC) to encrypt sensitive data:
const cipherText = await CryptoVault.aesGcmEncrypt('Hello', alias);
const plainText = await CryptoVault.aesGcmDecrypt(cipherText, alias);
///You can export keys securely in Base64 and restore them later:
const backup = await CryptoVault.backupKey(alias);
await CryptoVault.restoreKey(alias + '_restored', backup);
///Depending on policy, lock or unlock the vault:
await CryptoVault.lockVault();
const locked = await CryptoVault.isVaultLocked();
await CryptoVault.unlockVault(''); // policy NONE does not require data
console.log(await CryptoVault.ping()); // "pong"
console.log(await CryptoVault.echo('hello')); // "hello"
| Method | Parameters | Returns | Description & Flow Notes | |||
|---|---|---|---|---|---|---|
generateSecureKey(alias) |
alias: string |
Promise<string> |
Generates or retrieves a secure key. Must call before encryption/decryption | |||
aesGcmEncrypt(plainText, alias) |
plainText: string, alias: string |
Promise<string> |
AES-GCM encrypts plaintext. Requires key generated | |||
aesGcmDecrypt(cipherText, alias) |
cipherText: string, alias: string |
Promise<string> |
AES-GCM decrypts ciphertext. Requires key generated | |||
aesGcmEncryptWithHmac(plainText, keyBase64) |
plainText: string, keyBase64 |
Promise<string> |
Authenticated encryption. Requires Base64 key | |||
aesGcmDecryptWithHmac(cipherTextBase64, keyBase64) |
cipherTextBase64: string, keyBase64 |
Promise<string> |
Authenticated decryption. Requires Base64 key | |||
backupKey(alias) |
alias: string |
Promise<string> |
Exports key as Base64; key must exist | |||
restoreKey(alias, backupBlob) |
alias: string, backupBlob: string |
Promise<void> |
Restores key from Base64 backup | |||
lockVault() |
- | Promise<void> |
Locks vault; only affects methods if policy != NONE | |||
unlockVault(authData) |
authData: string |
Promise<void> |
Unlocks vault; authData used if policy requires | |||
isVaultLocked() |
- | Promise<boolean> |
Check if vault is currently locked | |||
setVaultPolicy(policy, timeoutMs?) |
`policy: 'NONE' | 'PIN' | 'BIOMETRIC' | 'TIMEOUT', timeoutMs?: number` | Promise<void> |
Sets vault policy; affects lock/unlock behavior |
hashString(message) |
message: string |
Promise<string> |
SHA-256 hash; independent method | |||
hmacSHA256(message, alias) |
message: string, alias: string |
Promise<string> |
HMAC; key must exist | |||
getRandomBytes(length) |
length: number |
Promise<string> |
Independent; cryptographically secure | |||
getRandomId() |
- | Promise<string> |
Generates UUID | |||
getDeviceInfo() |
- | Promise<string> |
Returns unique device ID | |||
ping() |
- | string |
Test connectivity | |||
echo(message) |
message: string |
string |
Returns the same message | |||
| Method | Parameters | Returns | Description & Flow Notes | |||
| ---------------------------------------------------------------- | --------------------------------------------------------------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |||
generateSecureKeyWithAuth(alias, authValiditySeconds?) |
alias: string, authValiditySeconds?: number |
Promise<string> |
Generates a key protected by biometrics. authValiditySeconds defines how long the key remains usable after authentication (default: -1 = one-time auth). Requires device fingerprint enrolled |
|||
aesGcmEncryptWithAuth(plainText, alias, authValiditySeconds?) |
plainText: string, alias: string, authValiditySeconds?: number |
Promise<string> |
Encrypts data using a key protected with biometric authentication. User must authenticate to use the key. | |||
aesGcmDecryptWithAuth(cipherText, alias, authValiditySeconds?) |
cipherText: string, alias: string, authValiditySeconds?: number |
Promise<string> |
Decrypts data using a key protected with biometric authentication. User must authenticate to access the key. |
Keys are never exported from Keystore / Keychain
AES-GCM uses random IV per encryption
HMAC ensures authenticated encryption to detect tampering
Always use unique aliases to avoid overwriting keys
Sensitive data should never be logged or exposed in memory
IOS Soon
Biometric-based key access (Face ID)
Fork the repo
Use TypeScript + TurboModules architecture
Follow existing code style and naming conventions
Add tests for any new feature
MIT © [thtRajasthaniGuy]