How to use the @hapi/cryptiles.randomBits function in @hapi/cryptiles

To help you get started, we’ve selected a few @hapi/cryptiles 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 hapijs / iron / test / index.js View on Github external
it('fails to turn object into a ticket (failed to stringify object)', async () => {

        const cyclic = [];
        cyclic[0] = cyclic;
        const key = Cryptiles.randomBits(128);
        const err = await expect(Iron.seal(cyclic, key, Iron.defaults)).to.reject(/Failed to stringify object: Converting circular structure to JSON/);
        expect(err.isBoom).to.be.true();
    });
github hapijs / statehood / test / index.js View on Github external
it('formats a header with server definition (iron + options, buffer password)', async () => {

            const definitions = new Statehood.Definitions();
            definitions.add('sid', { encoding: 'iron', password: Cryptiles.randomBits(256), iron: Iron.defaults });
            const header = await definitions.format({ name: 'sid', value: { a: 1, b: 2, c: 3 } });
            expect(header[0]).to.have.string('sid=Fe26.2*');
        });
github hapijs / iron / test / index.js View on Github external
it('turns object into a ticket than parses the ticket successfully (password buffer)', async () => {

        const key = Cryptiles.randomBits(256);
        const sealed = await Iron.seal(obj, key, Iron.defaults);
        const unsealed = await Iron.unseal(sealed, key, Iron.defaults);
        expect(unsealed).to.equal(obj);
    });
github hapijs / iron / test / index.js View on Github external
it('produces the same mac when used with buffer password', async () => {

            const data = 'Not so random';
            const key = Cryptiles.randomBits(256);
            const hmac = Crypto.createHmac(Iron.defaults.integrity.algorithm, key).update(data);
            const digest = hmac.digest('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');

            const mac = await Iron.hmacWithPassword(key, Iron.defaults.integrity, data);
            expect(mac.digest).to.equal(digest);
        });
    });
github hapijs / iron / test / index.js View on Github external
it('turns object into a ticket than parses the ticket successfully (password buffer in object)', async () => {

        const key = Cryptiles.randomBits(256);
        const sealed = await Iron.seal(obj, key, Iron.defaults);
        const unsealed = await Iron.unseal(sealed, { 'default': key }, Iron.defaults);
        expect(unsealed).to.equal(obj);
    });
github hapijs / iron / test / index.js View on Github external
it('handles separate password buffers (password object)', async () => {

        const key = {
            id: '1',
            encryption: Cryptiles.randomBits(256),
            integrity: Cryptiles.randomBits(256)
        };

        const sealed = await Iron.seal(obj, key, Iron.defaults);
        const unsealed = await Iron.unseal(sealed, { '1': key }, Iron.defaults);
        expect(unsealed).to.equal(obj);
    });
github hapijs / iron / test / index.js View on Github external
it('handles a common password buffer (password object)', async () => {

        const key = {
            id: '1',
            secret: Cryptiles.randomBits(256)
        };

        const sealed = await Iron.seal(obj, key, Iron.defaults);
        const unsealed = await Iron.unseal(sealed, { '1': key }, Iron.defaults);
        expect(unsealed).to.equal(obj);
    });
github hapijs / iron / test / index.ts View on Github external
import * as Iron from '..';
import * as Lab from '@hapi/lab';

const Cryptiles = require('@hapi/cryptiles');


const { expect } = Lab.types;


const password = 'some_not_random_password_that_is_also_long_enough';

const buffer = Cryptiles.randomBits(256);

const defaults = {
    encryption: {
        saltBits: 256,
        algorithm: 'aes-256-cbc',
        iterations: 1,
        minPasswordlength: 32
    },

    integrity: {
        saltBits: 256,
        algorithm: 'sha256',
        iterations: 1,
        minPasswordlength: 32
    },
github hapijs / iron / lib / index.js View on Github external
const randomSalt = Cryptiles.randomBits(options.saltBits);
            salt = randomSalt.toString('hex');
        }

        const derivedKey = await internals.pbkdf2(password, salt, options.iterations, algorithm.keyBits / 8, 'sha1');

        result.key = derivedKey;
        result.salt = salt;
    }

    if (options.iv) {
        result.iv = options.iv;
    }
    else if (algorithm.ivBits) {
        result.iv = Cryptiles.randomBits(algorithm.ivBits);
    }

    return result;
};
github hapijs / iron / lib / index.js View on Github external
result.key = password;
        result.salt = '';
    }
    else {
        if (password.length < options.minPasswordlength) {
            throw new Boom.Boom('Password string too short (min ' + options.minPasswordlength + ' characters required)');
        }

        let salt = options.salt;
        if (!salt) {
            if (!options.saltBits) {
                throw new Boom.Boom('Missing salt and saltBits options');
            }

            const randomSalt = Cryptiles.randomBits(options.saltBits);
            salt = randomSalt.toString('hex');
        }

        const derivedKey = await internals.pbkdf2(password, salt, options.iterations, algorithm.keyBits / 8, 'sha1');

        result.key = derivedKey;
        result.salt = salt;
    }

    if (options.iv) {
        result.iv = options.iv;
    }
    else if (algorithm.ivBits) {
        result.iv = Cryptiles.randomBits(algorithm.ivBits);
    }

@hapi/cryptiles

General purpose crypto utilities

BSD-3-Clause
Latest version published 1 year ago

Package Health Score

78 / 100
Full package analysis