How to use the tweetnacl.util 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 jo / pouch-box / test / index.js View on Github external
var test = require('tape')
var PouchDB = require('pouchdb')
var memdown = require('memdown')
var nacl = require('tweetnacl')

PouchDB.plugin(require('../'))

var keyPair = nacl.box.keyPair()
var permitId = 'permit/' + nacl.util.encodeBase64(keyPair.publicKey)
var dbname = 'test'

test('basics', function(t) {
  var db = new PouchDB(dbname, { db: memdown })
  var receiver

  db.box(keyPair)
    .then(function(permit) {
      receiver = permit.receiver()
      
      t.ok(permit.databaseKey.publicKey, 'returns database public key')
      t.ok(permit.databaseKey.secretKey, 'returns database secret key')
    })
    .then(function() {
      return db.put({ box: { foo: 'bar' } }, 'baz')
    })
github jo / pouch-box / test / lib / permit.js View on Github external
test('setup', function(t) {
  var publicKey = 'liofnczKPhM8awUO4/nzujz95StpmxZtZ/wseIlyxDM='
  var key = {
    publicKey: nacl.util.decodeBase64(publicKey)
  }

  var doc = permit(key)

  t.equal(doc._id, 'permit/' + publicKey,
    '_id is base64 encoded key, prepended with `permit/`')
  t.equal(doc.type, 'curve25519-xsalsa20-poly1305',
    'type is `curve25519-xsalsa20-poly1305`')
  
  t.end()
})
github MicroMinion / curvecp / examples / reference-test-client.js View on Github external
var net = require('net-udp')
var PacketStream = require('../src/packet-stream.js')
var MessageStream = require('../src/message-stream.js')
var nacl = require('tweetnacl')
nacl.util = require('tweetnacl-util')
var winston = require('winston')
var winstonWrapper = require('winston-meta-wrapper')

var logger = new winston.Logger({
  transports: [
    new winston.transports.Console({
      level: 'debug',
      timestamp: true,
      logstash: false
    })
  ]
})
logger = winstonWrapper(logger)

var keypair = nacl.box.keyPair()
var connection = new net.Socket()
github jo / pouch-box / lib / permit.js View on Github external
module.exports = function permit(sessionKey, databaseKey) {
  var permit = {
    _id: 'permit/' + nacl.util.encodeBase64(sessionKey.publicKey),
    type: 'curve25519-xsalsa20-poly1305',
    sessionKey: sessionKey,
    databaseKey: databaseKey || nacl.box.keyPair()
  }

  permit.toJSON = function() {
    return {
      _id: permit._id,
      _rev: permit._rev,
      type: permit.type,
      nonce: nacl.util.encodeBase64(permit.nonce),
      ephemeral: nacl.util.encodeBase64(permit.ephemeral),
      encryptedKey: nacl.util.encodeBase64(permit.encryptedKey)
    }
  }
github bogbook / bog / server.js View on Github external
var open = require('open')

var app = new koa()

app.use(serve({rootDir: '.', notFoundFile: 'index.html'}))

app.listen(config.port)

open(config.fullurl)

console.log('Bogbook is running at: ' + config.fullurl)

var bog = require('./bog')
var WS = require('ws')
var nacl = require('tweetnacl')
    nacl.util = require('tweetnacl-util')

var wserve = new WS.Server({ port: config.wsport })

bog.keys().then(key => {
  wserve.on('connection', function (ws) {
    ws.on('message', function (message) {
      var req = JSON.parse(message)
      if (req.sendpub) {
        ws.send(key.publicKey)
        ws.close()
      } else { 
        bog.unbox(req.box, req.requester, key).then(unboxed => {
          var unboxedreq = JSON.parse(unboxed)
          //console.log(unboxedreq)
          if (unboxedreq.seq >= 0) {
            printAsk(req, unboxedreq)
github ConsenSys / stow-js / src / util.js View on Github external
const genKeyPair = () => {
  const keys = nacl.box.keyPair();
  return {
    privateKey: nacl.util.encodeBase64(keys.secretKey),
    publicKey: nacl.util.encodeBase64(keys.publicKey),
  };
};
github 3box / muport-core-js / lib / keyring.js View on Github external
var symEncryptBase = function symEncryptBase(msg, symKey, nonce) {
  nonce = nonce || randomNonce();
  if (typeof msg === 'string') {
    msg = nacl.util.decodeUTF8(msg);
  }

  var ciphertext = nacl.secretbox(msg, nonce, symKey);

  return {
    nonce: nacl.util.encodeBase64(nonce),
    ciphertext: nacl.util.encodeBase64(ciphertext)
  };
};
github fluidtrends / chunky / cli / executors / carmel / utils.js View on Github external
function encrypt(account, cache, input) {
  const key = secureKey(cache)
  const keyUint8Array = nacl.util.decodeBase64(key.private)
  const nonce = nacl.randomBytes(nacl.secretbox.nonceLength)
  const messageUint8 = nacl.util.decodeUTF8(JSON.stringify(Object.assign({}, input)))
  const box = nacl.secretbox(messageUint8, nonce, keyUint8Array)

  const fullMessage = new Uint8Array(nonce.length + box.length)
  fullMessage.set(nonce)
  fullMessage.set(box, nonce.length)

  return nacl.util.encodeBase64(fullMessage)
}