How to use the tweetnacl.stream function in tweetnacl

To help you get started, we’ve selected a few tweetnacl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github carlos8f / salty / lib / ephemeral.js View on Github external
var nacl = require('tweetnacl')
  , crypto = require('crypto')
  , assert = require('assert')
  , constants = require('./constants')
  , a = require('../utils/a')
  , through = require('through')

nacl.stream = require('nacl-stream').stream

function createEphemeral (recipient, nonce, totalSize) {
  var boxKey = nacl.box.keyPair()
  var ephPk = Buffer.from(boxKey.publicKey)
  var ephSk = Buffer.from(boxKey.secretKey)
  return makeEphemeral(ephPk, nonce, totalSize, recipient.encryptPk, ephSk)
}

function makeEphemeral (ephPk, nonce, size, encryptPk, decryptSk) {
  var beforeResult = nacl.box.before(a(encryptPk), a(decryptSk))
  assert(beforeResult)
  var k = Buffer.from(beforeResult), encryptedLen, totalSize
  if (Buffer.isBuffer(size)) {
    // decrypt the message length
    encryptedLen = size
    var decryptResult = nacl.box.open.after(a(size), a(nonce), a(k))
github carlos8f / salty / lib / encrypt.js View on Github external
var fs = require('fs')
  , nacl = require('tweetnacl')
  , constants = require('../lib/constants')
  , libEphemeral = require('./ephemeral')
  , writeHeader = require('../utils/writeHeader')
  , makeNonce = require('../utils/makeNonce')
  , through = require('through')
  , assert = require('assert')
  , bs58 = require('bs58')

nacl.stream = require('nacl-stream').stream

function encrypt (inStream, recipient, nonce, totalSize, wallet, nopad, headers) {
  headers || (headers = {})
  var eph = libEphemeral.create(recipient, nonce, totalSize)
  var ended = false
  var encryptor = eph.createEncryptor(function isLast () {
    return ended
  })
  var bytesEncrypted = 0
  var header = {}
  var hashStream = eph.createHmac('sha256')
  var outStream = through()
  encryptor.on('data', function (chunk) {
    outStream.write(chunk)
  })
  encryptor.once('end', function () {