How to use the sodium-native.crypto_secretbox_KEYBYTES function in sodium-native

To help you get started, we’ve selected a few sodium-native 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 mcollina / fastify-secure-session / index.js View on Github external
salt,
      sodium.crypto_pwhash_OPSLIMIT_MODERATE,
      sodium.crypto_pwhash_MEMLIMIT_MODERATE,
      sodium.crypto_pwhash_ALG_DEFAULT)
  }

  if (options.key) {
    key = options.key
    if (typeof key === 'string') {
      key = Buffer.from(key, 'base64')
    } else if (!(key instanceof Buffer)) {
      return next(new Error('key must be a string or a Buffer'))
    }

    if (key.length < sodium.crypto_secretbox_KEYBYTES) {
      return next(new Error(`key must be at least ${sodium.crypto_secretbox_KEYBYTES} bytes`))
    }
  }

  if (!key) {
    return next(new Error('key or secret must specified'))
  }

  const cookieName = options.cookieName || 'session'
  const cookieOptions = options.cookieOptions || options.cookie || {}

  // just to add something to the shape
  // TODO verify if it helps the perf
  fastify.decorateRequest('session', null)

  fastify
    .register(require('fastify-cookie'))
github mcollina / fastify-secure-session / index.js View on Github external
Buffer.from(options.secret),
      salt,
      sodium.crypto_pwhash_OPSLIMIT_MODERATE,
      sodium.crypto_pwhash_MEMLIMIT_MODERATE,
      sodium.crypto_pwhash_ALG_DEFAULT)
  }

  if (options.key) {
    key = options.key
    if (typeof key === 'string') {
      key = Buffer.from(key, 'base64')
    } else if (!(key instanceof Buffer)) {
      return next(new Error('key must be a string or a Buffer'))
    }

    if (key.length < sodium.crypto_secretbox_KEYBYTES) {
      return next(new Error(`key must be at least ${sodium.crypto_secretbox_KEYBYTES} bytes`))
    }
  }

  if (!key) {
    return next(new Error('key or secret must specified'))
  }

  const cookieName = options.cookieName || 'session'
  const cookieOptions = options.cookieOptions || options.cookie || {}

  // just to add something to the shape
  // TODO verify if it helps the perf
  fastify.decorateRequest('session', null)

  fastify
github mcollina / fastify-secure-session / test / path.js View on Github external
'use strict'

const t = require('tap')
const fastify = require('fastify')({ logger: false })
const sodium = require('sodium-native')
const cookie = require('cookie')
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)

sodium.randombytes_buf(key)

fastify.register(require('../'), {
  key,
  cookie: {
    path: '/'
  }
})

t.tearDown(fastify.close.bind(fastify))
t.plan(4)

fastify.post('/auth', (request, reply) => {
  request.session.set('data', request.body)
  reply.send('hello world')
github mcollina / fastify-secure-session / test / key.js View on Github external
'use strict'

const t = require('tap')
const fastify = require('fastify')({ logger: false })
const sodium = require('sodium-native')
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)

sodium.randombytes_buf(key)

fastify.register(require('../'), {
  key
})

fastify.post('/', (request, reply) => {
  request.session.set('data', request.body)
  reply.send('hello world')
})

t.tearDown(fastify.close.bind(fastify))
t.plan(5)

fastify.get('/', (request, reply) => {
github openkfw / TruBudget / api / src / organization / vault.ts View on Github external
function toKeyBuffer(secret: string): Buffer {
  if (secret.length > sodium.crypto_secretbox_KEYBYTES) {
    winston.warn(
      `truncate secret with length ${secret.length} to length ${sodium.crypto_secretbox_KEYBYTES}`,
    );
  }

  const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES);
  key.write(secret.slice(0, sodium.crypto_secretbox_KEYBYTES));

  return key;
}
github mafintosh / sodium-encryption / sodium.js View on Github external
exports.key = function () {
  return randomBytes(sodium.crypto_secretbox_KEYBYTES)
}
github samuelmaddock / swarm-peer-server / lib / encryption.js View on Github external
exports.key = function () {
  return randomBytes(sodium.crypto_secretbox_KEYBYTES)
}
github openkfw / TruBudget / api / src / organization / vault.ts View on Github external
function toKeyBuffer(secret: string): Buffer {
  if (secret.length > sodium.crypto_secretbox_KEYBYTES) {
    winston.warn(
      `truncate secret with length ${secret.length} to length ${sodium.crypto_secretbox_KEYBYTES}`,
    );
  }

  const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES);
  key.write(secret.slice(0, sodium.crypto_secretbox_KEYBYTES));

  return key;
}
github openkfw / TruBudget / api / src / organization / vault.ts View on Github external
function toKeyBuffer(secret: string): Buffer {
  if (secret.length > sodium.crypto_secretbox_KEYBYTES) {
    winston.warn(
      `truncate secret with length ${secret.length} to length ${sodium.crypto_secretbox_KEYBYTES}`,
    );
  }

  const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES);
  key.write(secret.slice(0, sodium.crypto_secretbox_KEYBYTES));

  return key;
}
github mcollina / fastify-secure-session / genkey.js View on Github external
#! /usr/bin/env node

'use strict'

const sodium = require('sodium-native')
const buf = Buffer.allocUnsafe(sodium.crypto_secretbox_KEYBYTES)
sodium.randombytes_buf(buf)
process.stdout.write(buf)