Sha256: 5c771e93e171b8a1137238523a8ea3fe903560d3eed2db40b97ef8f98372ab00

Contents?: true

Size: 1.33 KB

Versions: 109

Compression:

Stored size: 1.33 KB

Contents

// Load modules

var Crypto = require('crypto');
var Boom = require('boom');


// Declare internals

var internals = {};


// Generate a cryptographically strong pseudo-random data

exports.randomString = function (size) {

    var buffer = exports.randomBits((size + 1) * 6);
    if (buffer instanceof Error) {
        return buffer;
    }

    var string = buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
    return string.slice(0, size);
};


exports.randomBits = function (bits) {

    if (!bits ||
        bits < 0) {

        return Boom.internal('Invalid random bits count');
    }

    var bytes = Math.ceil(bits / 8);
    try {
        return Crypto.randomBytes(bytes);
    }
    catch (err) {
        return Boom.internal('Failed generating random bits: ' + err.message);
    }
};


// Compare two strings using fixed time algorithm (to prevent time-based analysis of MAC digest match)

exports.fixedTimeComparison = function (a, b) {

    if (typeof a !== 'string' ||
        typeof b !== 'string') {

        return false;
    }

    var mismatch = (a.length === b.length ? 0 : 1);
    if (mismatch) {
        b = a;
    }

    for (var i = 0, il = a.length; i < il; ++i) {
        var ac = a.charCodeAt(i);
        var bc = b.charCodeAt(i);
        mismatch |= (ac ^ bc);
    }

    return (mismatch === 0);
};


Version data entries

109 entries across 101 versions & 13 rubygems

Version Path
ilog-0.4.1 node_modules/cryptiles/lib/index.js
ilog-0.4.0 node_modules/cryptiles/lib/index.js
ilog-0.3.3 node_modules/cryptiles/lib/index.js
optimacms-0.4.3 spec/dummy/node_modules/cryptiles/lib/index.js
optimacms-0.4.2 spec/dummy/node_modules/cryptiles/lib/index.js
locomotivecms-3.4.0 app/javascript/node_modules/cryptiles/lib/index.js
affiliator-0.2.1 node_modules/fsevents/node_modules/cryptiles/lib/index.js
lanes-0.8.0 node_modules/cryptiles/lib/index.js
lanes-0.8.0 node_modules/fsevents/node_modules/cryptiles/lib/index.js
trusty-festivity-extension-2.6.3 node_modules/bower/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
trusty-festivity-extension-2.6.2 node_modules/bower/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
node-compiler-0.9.0 vendor/node-v7.2.1/deps/npm/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
select_all-rails-0.3.1 node_modules/cryptiles/lib/index.js
trusty-festivity-extension-2.6.1 node_modules/bower/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
node-compiler-0.8.0 vendor/node-v7.2.0/deps/npm/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
trusty-festivity-extension-2.6 node_modules/bower/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
node-compiler-0.7.0 vendor/node-v6.9.1/deps/npm/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
node-compiler-0.7.0 vendor/node-v7.1.0/deps/npm/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js
trusty-festivity-extension-2.5.19 node_modules/bower/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js