react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / hawk / node_modules / cryptiles / lib / index.js
81154 views// Load modules12var Crypto = require('crypto');3var Boom = require('boom');456// Declare internals78var internals = {};91011// Generate a cryptographically strong pseudo-random data1213exports.randomString = function (size) {1415var buffer = exports.randomBits((size + 1) * 6);16if (buffer instanceof Error) {17return buffer;18}1920var string = buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');21return string.slice(0, size);22};232425exports.randomBits = function (bits) {2627if (!bits ||28bits < 0) {2930return Boom.internal('Invalid random bits count');31}3233var bytes = Math.ceil(bits / 8);34try {35return Crypto.randomBytes(bytes);36}37catch (err) {38return Boom.internal('Failed generating random bits: ' + err.message);39}40};414243// Compare two strings using fixed time algorithm (to prevent time-based analysis of MAC digest match)4445exports.fixedTimeComparison = function (a, b) {4647if (typeof a !== 'string' ||48typeof b !== 'string') {4950return false;51}5253var mismatch = (a.length === b.length ? 0 : 1);54if (mismatch) {55b = a;56}5758for (var i = 0, il = a.length; i < il; ++i) {59var ac = a.charCodeAt(i);60var bc = b.charCodeAt(i);61mismatch |= (ac ^ bc);62}6364return (mismatch === 0);65};6667686970