hashon
v2.2.8
Published
Encrypt, decrypt and hash JSON data with AES and SHA — made for secure local files and syncing.
Maintainers
Readme
# hashon



A simple Node.js library for automatically encrypting, decrypting, and hashing JSON data using CryptoJS.
---
## Installation
```bash
npm install hashonUsage (async/await)
Use the async API to automatically keep encrypted .sec.json files in sync with your plaintext .json files.
Example usage with Express:
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const { decrypt, autoEncryptIfChanged } = require('hashon');
require('dotenv').config();
const app = express();
const port = 3001;
// 🔹 Ensure SECRET_KEY is provided
const SECRET_KEY = process.env.SECRET_KEY;
if (!SECRET_KEY) {
console.error("❌ SECRET_KEY missing in .env file. Please add it for encryption/decryption.");
process.exit(1);
}
const dataPath = path.join(__dirname, 'data', 'data.json');
const securePath = dataPath.replace(/\.json$/, '.sec.json');
// Automatically encrypt and sync .sec.json file on startup
autoEncryptIfChanged(dataPath, SECRET_KEY).catch(console.error);
// Serve plaintext JSON
app.get('/api/data', async (req, res) => {
try {
const raw = await fs.readFile(dataPath, 'utf-8');
res.type('application/json').send(raw);
} catch {
res.status(500).json({ error: 'Could not read file' });
}
});
// Serve decrypted JSON from encrypted file
app.get('/api/secure-data', async (req, res) => {
try {
const encrypted = await fs.readFile(securePath, 'utf-8');
const json = decrypt(encrypted, SECRET_KEY);
res.json(json);
} catch (err) {
console.error('[server] Decryption error:', err);
res.status(500).json({ error: 'Decryption failed or file missing' });
}
});
app.listen(port, () => {
console.log(`[server] Listening on http://localhost:${port}`);
});How It Works
- You edit
data.jsonas normal. hashondetects changes and automatically updatesdata.sec.jsonasynchronously.- This ensures your encrypted data is always up to date without blocking your app.
About data.json and data.sec.json
data.jsonis your editable, plaintext JSON file.data.sec.jsonis the AES-encrypted counterpart automatically managed byhashon.
Features
- 🔐 AES encryption with optional secret key.
- 🔒 SHA-512 hashing with
$HASH$prefix tagging. - ⚙️ Async, non-blocking file encryption and syncing.
- 🧩 Easy integration in any Node.js or Express application.
Setting the Secret Key
Create a .env file in your project root:
SECRET_KEY=your_very_secret_passwordIf not set, a default insecure fallback key is used (do not use this in production).
⚠️ Always set a secure
SECRET_KEYfor production environments.
License
MIT
Changelog
See CHANGELOG.md for details on recent changes and releases.
Contributions
Feel free to open issues or submit pull requests on GitHub!
Made by Felix Lind
