Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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'))
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
'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')
'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) => {
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;
}
exports.key = function () {
return randomBytes(sodium.crypto_secretbox_KEYBYTES)
}
exports.key = function () {
return randomBytes(sodium.crypto_secretbox_KEYBYTES)
}
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;
}
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;
}
#! /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)