https://github.com/little-core-labs/hypercore-xsalsa20-onwrite-hook
A write hook to encrypt/decrypt data using a XSalsa20 cipher into a hypercore storage when replicating from peers
https://github.com/little-core-labs/hypercore-xsalsa20-onwrite-hook
hook hypercore write xsalsa20
Last synced: 11 months ago
JSON representation
A write hook to encrypt/decrypt data using a XSalsa20 cipher into a hypercore storage when replicating from peers
- Host: GitHub
- URL: https://github.com/little-core-labs/hypercore-xsalsa20-onwrite-hook
- Owner: little-core-labs
- License: mit
- Created: 2019-05-06T15:21:43.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-02-04T12:08:58.000Z (over 6 years ago)
- Last Synced: 2025-06-28T16:03:51.425Z (11 months ago)
- Topics: hook, hypercore, write, xsalsa20
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
hypercore-xsalsa20-onwrite-hook
===============================
> A write hook to decrypt data using a xsalsa20 cipher into a Hypercore
> storage when replicating from peers.
## Installation
```sh
$ npm install hypercore-xsalsa20-onwrite-hook
```
## Usage
```js
const onwrite = hook(sharedSecret)
```
## Example
```js
const replicate = require('hypercore-replicate')
const hypercore = require('hypercore')
const crypto = require('hypercore-crypto')
const pump = require('pump')
const hook = require('hypercore-xsalsa20-onwrite-hook')
const ram = require('random-access-memory')
const key = crypto.randomBytes(32)
const { publicKey, secretKey } = crypto.keyPair()
const source = hypercore(ram, publicKey, {
secretKey,
})
const cipher = hypercore(ram, publicKey, {
secretKey,
onwrite: hook(key)
})
const edge = hypercore(ram, publicKey)
const reader = hypercore(ram, publicKey, {
onwrite: hook(key)
})
source.append(Buffer.from('hello'), (err) => {
source.head(console.log) // plaintext
// load cipher hypercore
pump(source.createReadStream(), cipher.createWriteStream(), (err) => {
if (err) throw err
cipher.head(console.log) // ciphertext
replicate(cipher, edge, (err) => {
if (err) throw err
edge.head(console.log) // ciphertext
replicate(edge, reader, (err) => {
if (err) throw err
reader.head(console.log) // plaintext
})
})
})
})
```
## API
### `const onwrite = hook(nonceStorage, sharedKey)`
Creates a `onwrite()` hook for a Hypercore feed that uses the
xsalsa20 cipher to encipher or decipher blocks in a Hypercore feed.
Nonces are computed from the Hypercore feed's public key and block
index.
## License
MIT