@deadmansswitch/encryption
v1.0.0
Published
Cross-platform encryption library for React Native and React web applications with password-based encryption
Maintainers
Readme
@deadmansswitch/encryption
Cross-Platform Encryption Library
A lightweight, cross-platform encryption library that works seamlessly with both React Native and React web applications. Uses password-based encryption with PBKDF2 key derivation and AES-GCM encryption.
Features
- Cross-platform: Works in React Native and React web/Next.js
- Password-based encryption: Secure PBKDF2 key derivation
- AES-GCM encryption: Strong encryption with built-in authentication
- TypeScript support: Full type definitions included
- React Native: Encryption and decryption
- React Web: Decryption only (as requested)
Installation
npm install @deadmansswitch/encryptionFor React Native, you'll also need to install the crypto dependency:
npm install react-native-quick-cryptoAfter installation, you'll need to complete the platform setup. For React Native 0.60+, run:
cd ios && pod installUsage
React Native (Encryption + Decryption)
import { encrypt, decrypt } from '@deadmansswitch/encryption';
// Encrypt data
const encryptData = async () => {
const result = await encrypt('Hello, World!', {
password: 'your-secure-password'
});
console.log('Encrypted:', result.encryptedData);
console.log('Salt:', result.salt);
console.log('IV:', result.iv);
return result;
};
// Decrypt data
const decryptData = async (encryptionResult) => {
const decrypted = await decrypt({
password: 'your-secure-password',
encryptedData: encryptionResult.encryptedData,
salt: encryptionResult.salt,
iv: encryptionResult.iv
});
console.log('Decrypted:', decrypted); // "Hello, World!"
return decrypted;
};React Web/Next.js (Decryption Only)
import { decrypt } from '@deadmansswitch/encryption';
// Decrypt data received from React Native
const decryptData = async () => {
const decrypted = await decrypt({
password: 'your-secure-password',
encryptedData: 'encrypted-data-from-rn',
salt: 'salt-from-rn',
iv: 'iv-from-rn'
});
console.log('Decrypted:', decrypted);
return decrypted;
};API Reference
encrypt(data: string, options: EncryptionOptions): Promise<EncryptionResult>
Encrypts a string using password-based encryption.
Parameters:
data- The string to encryptoptions- Encryption optionspassword- The password to use for encryptioniterations?- Number of PBKDF2 iterations (default: 100000)keyLength?- Key length in bits (default: 256)
Returns: Promise resolving to an EncryptionResult containing:
encryptedData- Base64 encoded encrypted datasalt- Base64 encoded saltiv- Base64 encoded initialization vector
decrypt(options: DecryptionOptions): Promise<string>
Decrypts data using password-based decryption.
Parameters:
options- Decryption optionspassword- The password used for encryptionencryptedData- Base64 encoded encrypted datasalt- Base64 encoded saltiv- Base64 encoded initialization vectoriterations?- Number of PBKDF2 iterations (default: 100000)keyLength?- Key length in bits (default: 256)
Returns: Promise resolving to the decrypted string
Security Notes
- Uses PBKDF2 with 100,000 iterations by default
- Uses AES-GCM for authenticated encryption
- Generates random salt and IV for each encryption
- Password should be strong and stored securely
- Never hardcode passwords in your application
Platform Detection
The library automatically detects the platform and uses the appropriate implementation:
- React Native: Uses
react-native-quick-cryptofor native performance - Web/Next.js: Uses Web Crypto API
TypeScript
Full TypeScript support is included with exported interfaces:
import type {
EncryptionOptions,
EncryptionResult,
DecryptionOptions
} from '@deadmansswitch/encryption';License
MIT
