UNPKG

hashon

Version:

Encrypt, decrypt and hash JSON data with AES and SHA — made for secure local files and syncing.

138 lines (93 loc) 3.22 kB
````md # hashon ![npm version](https://img.shields.io/npm/v/hashon.svg) ![npm downloads](https://img.shields.io/npm/dm/hashon.svg) ![license](https://img.shields.io/npm/l/hashon.svg?cacheBust=1) A simple Node.js library for automatically encrypting, decrypting, and hashing JSON data using CryptoJS. --- ## Installation ```bash npm install hashon ```` --- ## Usage (async/await) Use the async API to automatically keep encrypted `.sec.json` files in sync with your plaintext `.json` files. Example usage with Express: ```js 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.json` as normal. * `hashon` detects changes and automatically updates `data.sec.json` asynchronously. * This ensures your encrypted data is always up to date without blocking your app. --- ## About `data.json` and `data.sec.json` * `data.json` is your editable, plaintext JSON file. * `data.sec.json` is the AES-encrypted counterpart automatically managed by `hashon`. --- ## 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: ```env SECRET_KEY=your_very_secret_password ``` If not set, a default insecure fallback key is used (do **not** use this in production). > ⚠️ Always set a secure `SECRET_KEY` for production environments. --- ## License MIT --- ## Changelog See [CHANGELOG.md](./CHANGELOG.md) for details on recent changes and releases. --- ## Contributions Feel free to open issues or submit pull requests on GitHub! [https://github.com/FelixLind1](https://github.com/FelixLind1) --- **Made by Felix Lind** ```