Secure Storage Middleware for React Native Applications
React Native Encrypted Async Storage is a secure storage library that acts as a middleware, combining the strengths of:
-
rn-encryption
: Provides encryption capabilities for secure data handling. -
react-native-async-storage
: A robust, community-driven storage solution for React Native applications.
This library ensures all your storage operations, such as setting, getting, and merging data, are encrypted securely using industry-standard AES-GCM encryption.
npm i @rrishuyadav/react-native-encrypted-async-storage
# or
yarn add @rrishuyadav/react-native-encrypted-async-storage
Ensure rn-encryption
and react-native-async-storage
are also installed as dependencies.
npm install rn-encryption @react-native-async-storage/async-storage
- 🔒 AES-GCM Encryption: Ensures your data is encrypted and secure at rest.
- ⚡ Async Operations: Fully supports asynchronous operations for all storage methods.
- 🗝️ Key Management: Seamless encryption key management.
- 🧩 Data Merge: Merge complex JSON objects while maintaining encryption.
- 📊 Batch Operations: Efficient batch operations like
multiSet
,multiGet
, andmultiRemove
.
Store and Retrieve Encrypted Data
await EncryptedAsyncStorage.setItem('testKey', 'Hello, Encrypted World!');
const value = await EncryptedAsyncStorage.getItem('testKey');
console.log('Decrypted Value:', value); // Output: Hello, Encrypted World!
Remove an Item from Storage
await EncryptedAsyncStorage.removeItem('testKey');
const value = await EncryptedAsyncStorage.getItem('testKey');
console.log('Value after removeItem:', value); // Output: null
Store and Retrieve Multiple Encrypted Items
await EncryptedAsyncStorage.multiSet([
['key1', 'value1'],
['key2', 'value2'],
]);
const values = await EncryptedAsyncStorage.multiGet(['key1', 'key2']);
console.log('MultiGet Values:', values);
// Output: [['key1', 'value1'], ['key2', 'value2']]
Retrieve All Keys from Storage
const keys = await EncryptedAsyncStorage.getAllKeys();
console.log('All Keys:', keys);
// Output: ['key1', 'key2']
Remove Multiple Items
await EncryptedAsyncStorage.multiRemove(['key1', 'key2']);
const keysAfterRemove = await EncryptedAsyncStorage.getAllKeys();
console.log('Keys after multiRemove:', keysAfterRemove);
// Output: []
Merge JSON Data for a Single Item
await EncryptedAsyncStorage.setItem('mergeKey', JSON.stringify({ name: 'John' }));
await EncryptedAsyncStorage.mergeItem('mergeKey', JSON.stringify({ age: 30 }));
const mergedValue = await EncryptedAsyncStorage.getItem('mergeKey');
console.log('Merged Value:', mergedValue);
// Output: {"name": "John", "age": 30}
Merge JSON Data for Multiple Items
await EncryptedAsyncStorage.multiSet([
['user1', JSON.stringify({ name: 'Alice' })],
['user2', JSON.stringify({ name: 'Bob' })],
]);
await EncryptedAsyncStorage.multiMerge([
['user1', JSON.stringify({ age: 25 })],
['user2', JSON.stringify({ age: 28 })],
]);
const mergedUsers = await EncryptedAsyncStorage.multiGet(['user1', 'user2']);
console.log('MultiMerged Users:', mergedUsers);
// Output: [['user1', '{"name": "Alice", "age": 25}'], ['user2', '{"name": "Bob", "age": 28}']]
import EncryptedAsyncStorage from 'react-native-encrypted-asyncstorage';
useEffect(() => {
const init = async () => {
try {
console.log('--- Testing EncryptedAsyncStorage ---');
// Set and Get Item
await EncryptedAsyncStorage.setItem('testKey', 'Hello, Encrypted World!');
const value = await EncryptedAsyncStorage.getItem('testKey');
console.log('Decrypted Value:', value);
// Remove Item
await EncryptedAsyncStorage.removeItem('testKey');
// MultiSet & MultiGet
await EncryptedAsyncStorage.multiSet([
['key1', 'value1'],
['key2', 'value2'],
]);
const multiValues = await EncryptedAsyncStorage.multiGet(['key1', 'key2']);
console.log('MultiGet Values:', multiValues);
// Merge Items
await EncryptedAsyncStorage.setItem('mergeKey', JSON.stringify({ name: 'John' }));
await EncryptedAsyncStorage.mergeItem('mergeKey', JSON.stringify({ age: 30 }));
const mergedValue = await EncryptedAsyncStorage.getItem('mergeKey');
console.log('Merged Value:', mergedValue);
console.log('--- EncryptedAsyncStorage Test Completed Successfully ---');
} catch (error) {
console.error('Error in EncryptedAsyncStorage:', error);
}
};
init();
}, []);
- AES-GCM Encryption: Industry-standard AES encryption ensures data confidentiality and integrity.
- Key Management: Keys are securely stored and retrieved.
- Data Integrity: Prevents tampering by verifying encrypted payloads.
No additional configuration is required. Install the library and start using the API.
- Always validate inputs before encrypting and storing them.
- Avoid storing highly sensitive data in Async Storage; use native keychain solutions for secrets.
-
Issue: Data not decrypting correctly.
Solution: Ensure encryption keys are not rotated or lost. -
Issue: Merging fails after encryption.
Solution: Always fetch, decrypt, and merge JSON objects before re-encrypting.
- Fork the repository.
- Create a feature branch:
git checkout -b feature/your-feature
. - Commit changes:
git commit -m "Add your feature"
. - Push to the branch:
git push origin feature/your-feature
. - Open a pull request.
MIT License. See the LICENSE file for details.
Hey there! 👋 I'm Rishabh, the creator of React Native Encrypted Async Storage – a library designed to make secure storage seamless and reliable in React Native applications.
I'm passionate about building innovative mobile and web solutions, mentoring teams, and solving real-world problems with clean and efficient code.
- 💌 Email me: your.email@example.com
- 💼 Connect on LinkedIn: linkedin.com/in/rishabhyadav
- 🧑💻 GitHub: github.com/your-username
- 💬 Let's Chat: Always happy to discuss tech, startups, or new ideas!
Please do not hesitate to contact me in case some modification or additional functionality required from the library.
Looking forward to hearing from you! 🚀✨