Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gunar/json-crypto
Safe defaults for Node's crypto module
https://github.com/gunar/json-crypto
Last synced: about 2 months ago
JSON representation
Safe defaults for Node's crypto module
- Host: GitHub
- URL: https://github.com/gunar/json-crypto
- Owner: gunar
- Created: 2020-10-12T17:20:55.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T14:36:56.000Z (about 4 years ago)
- Last Synced: 2024-10-10T19:51:32.864Z (3 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# json-crypto
Easy to use abstraction on top of Node's native `crypto` module to safely encrypt and decrypt JSON objects.
## Installation
```
npm install json-crypto
```## Usage
```js
const { createKey, createCodec } = require('json-crypto');// Create a random encryption key
const key = createKey();// Create our codec with { encrypt, decrypt }
const codec = createCodec(key);// Encrypt a JSON object and return the ciphertext
const ciphertext = codec.encrypt({ message: 'my secret message' })// Decrypt the ciphertext back into a JSON object
const secret = codec.decrypt(ciphertext)
// secret === { message: 'my secret message' }
```NB: You'll probably want to persist the generated key and store it somewhere safe.
## Benefits
- Safe by nature (only uses native `crypto` functions)
- Safer by forcing you to use unique nonces ([see why it matters](https://www.cryptofails.com/post/70059609995/crypto-noobs-1-initialization-vectors))
- Easy to use interface for JSON objects
- Ciphertexts are valid URL query argument strings (using encodeURIComponent)