Skip to content

Commit

Permalink
box -> encrypt, unbox -> decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdinosaur committed Nov 22, 2023
1 parent 4bd25a9 commit 82abdaf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ replacement for [`pull-box-stream`](https://github.com/dominictarr/pull-box-stre
```js
const { randomBytes } = require('crypto')
const pull = require('pull-stream')
const { KEYBYTES, createBoxStream, createUnboxStream } = require('pull-secretstream')
const { KEYBYTES, createEncryptStream, createDecryptStream } = require('pull-secretstream')

// generate a random secret, `KEYBYTES` bytes long.
const key = randomBytes(KEYBYTES)
Expand All @@ -18,15 +18,15 @@ pull(
pull.values([plaintext1]),

// encrypt every byte
createBoxStream(key),
createEncryptStream(key),

// the encrypted stream
pull.through((ciphertext) => {
console.log('Encrypted: ', ciphertext)
}),

//decrypt every byte
createUnboxStream(key),
createDecryptStream(key),

pull.concat((err, plaintext2) => {
if (err) throw err
Expand Down
6 changes: 3 additions & 3 deletions examples/readme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { randomBytes } = require('crypto')
const pull = require('pull-stream')
const { KEYBYTES, createBoxStream, createUnboxStream } = require('../')
const { KEYBYTES, createEncryptStream, createDecryptStream } = require('../')

// generate a random secret, `KEYBYTES` bytes long.
const key = randomBytes(KEYBYTES)
Expand All @@ -11,15 +11,15 @@ pull(
pull.values([plaintext1]),

// encrypt every byte
createBoxStream(key),
createEncryptStream(key),

// the encrypted stream
pull.through((ciphertext) => {
console.log('Encrypted: ', ciphertext)
}),

//decrypt every byte
createUnboxStream(key),
createDecryptStream(key),

pull.concat((err, plaintext2) => {
if (err) throw err
Expand Down
30 changes: 15 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,41 @@ const pullHeader = require('pull-header')

module.exports = {
KEYBYTES,
createBoxStream,
createUnboxStream,
createEncryptStream,
createDecryptStream,
}

function createBoxStream(key) {
const boxer = new Push(key)
function createEncryptStream(key) {
const encrypter = new Push(key)

const sendHeader = pull.values([boxer.header])
const sendHeader = pull.values([encrypter.header])

const boxMap = pullMapLast(
const encryptMap = pullMapLast(
(plaintext) => {
return boxer.next(plaintext)
return encrypter.next(plaintext)
},
() => {
return boxer.final()
return encrypter.final()
},
)

return (stream) => {
return pullCat([sendHeader, pull(stream, boxMap)])
return pullCat([sendHeader, pull(stream, encryptMap)])
}
}

function createUnboxStream(key) {
const unboxer = new Pull(key)
function createDecryptStream(key) {
const decrypter = new Pull(key)

const receiveHeader = pullHeader(HEADERBYTES, (header) => {
unboxer.init(header)
decrypter.init(header)
})

const unboxMap = pullMap((ciphertext) => {
return unboxer.next(ciphertext)
const decryptMap = pullMap((ciphertext) => {
return decrypter.next(ciphertext)
})

return (stream) => {
return pull(stream, receiveHeader, unboxMap)
return pull(stream, receiveHeader, decryptMap)
}
}
8 changes: 4 additions & 4 deletions test/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const test = require('node:test')
const assert = require('node:assert')
const pull = require('pull-stream')
const { randomBytes } = require('crypto')
const { KEYBYTES, createBoxStream, createUnboxStream } = require('../')
const { KEYBYTES, createEncryptStream, createDecryptStream } = require('../')

test('test basic boxStream and unboxStream', async (t) => {
test('test basic encryptStream and decryptStream', async (t) => {
// generate a random secret, `KEYBYTES` bytes long.
const key = randomBytes(KEYBYTES)

Expand All @@ -13,11 +13,11 @@ test('test basic boxStream and unboxStream', async (t) => {
await new Promise((resolve, reject) => {
pull(
pull.values([plaintext1]),
createBoxStream(key),
createEncryptStream(key),
pull.through((ciphertext) => {
console.log('Encrypted: ', ciphertext)
}),
createUnboxStream(key),
createDecryptStream(key),
pull.concat((err, plaintext2) => {
if (err) return reject(err)
assert.equal(plaintext2.toString('ascii'), plaintext1.toString('ascii'))
Expand Down

0 comments on commit 82abdaf

Please sign in to comment.