secure-json-encryptor
v1.0.4
Published
A zero-dependency library for secure JSON encryption/decryption using Web Crypto API
Maintainers
Readme
Secure JSON Encryptor 🔐
A lightweight, zero-dependency library for secure JSON encryption and decryption using the native Web Crypto API. Works seamlessly across React Native, Node.js, and modern browsers.
✨ Features
- 🔒 Zero Dependencies — Built entirely on the native Web Crypto API (no third-party libraries required)
- 🔐 AES-GCM Encryption - Authenticated encryption with 256-bit keys
- 📱 Cross-Platform - React Native, Node.js, and browser compatible
- 🚀 Simple API - Easy-to-use encrypt/decrypt functions
- 📦 Ultra Lightweight - Less than 2KB minified
- 🛡️ TypeScript Ready - Includes full TypeScript definitions
- 🧪 Well Tested - Comprehensive test coverage
🌐 Platform Compatibility
Supports all environments with Web Crypto API:
- ✅ Android (React Native 0.71+)
- ✅ iOS (React Native 0.71+)
- Chrome 37+
- Firefox 34+
- Safari 14+
- Edge 79+
- Node.js 15+
📦 Installation
npm install secure-json-encryptor
# or
yarn add secure-json-encryptor
# or
pnpm add secure-json-encryptor
📦 Usaging
import { encrypt, decrypt } from 'secure-json-encryptor';
const secretKey = 'your-secret-password-here';
const sensitiveData = {
username: 'john_doe',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
preferences: { theme: 'dark', notifications: true }
};
// Encrypt data
const encrypted = await encrypt(sensitiveData, secretKey);
console.log('Encrypted:', encrypted); // Base64 string
// Decrypt data
const decrypted = await decrypt(encrypted, secretKey);
console.log('Decrypted:', decrypted); // Original object📱 React Native Example
import React, { useState } from 'react';
import { encrypt, decrypt } from 'secure-json-encryptor';
import AsyncStorage from '@react-native-async-storage/async-storage';
const App = () => {
const [data, setData] = useState(null);
const saveSecureData = async (userData) => {
const SECRET_KEY = 'your-app-secret-key';
// Encrypt before storing
const encrypted = await encrypt(userData, SECRET_KEY);
await AsyncStorage.setItem('user_data', encrypted);
};
const loadSecureData = async () => {
const SECRET_KEY = 'your-app-secret-key';
const encrypted = await AsyncStorage.getItem('user_data');
if (encrypted) {
try {
const decrypted = await decrypt(encrypted, SECRET_KEY);
setData(decrypted);
} catch (error) {
console.error('Failed to decrypt:', error);
}
}
};
// Component implementation...
};🖥️ Node.js Example
const { encrypt, decrypt } = require('secure-json-encryptor');
const fs = require('fs').promises;
// Encrypt and save sensitive config
async function saveConfig(config) {
const secretKey = process.env.APP_SECRET_KEY;
const encrypted = await encrypt(config, secretKey);
await fs.writeFile('config.encrypted', encrypted);
}
// Load and decrypt config
async function loadConfig() {
const secretKey = process.env.APP_SECRET_KEY;
const encrypted = await fs.readFile('config.encrypted', 'utf8');
return await decrypt(encrypted, secretKey);
}🔐 Security Best Practices
Key Management:
javascript
// Good: Use environment variables
const secretKey = process.env.ENCRYPTION_SECRET;
// Bad: Hardcoded keys
const secretKey = 'my-hardcoded-key';📄 License
Copyright (c) 2024 Secure JSON Encryptor
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
[https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)
Unless required by applicable law or agreed to in writing, this software
is provided on an "AS IS" basis, without any warranties or conditions of any kind,
either express or implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose, and non-infringement.
See the License for the specific language governing permissions and
limitations under the License.