"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/cryptiles/lib/index.js" (7 Feb 2017, 1364 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Load modules
    2 
    3 var Crypto = require('crypto');
    4 var Boom = require('boom');
    5 
    6 
    7 // Declare internals
    8 
    9 var internals = {};
   10 
   11 
   12 // Generate a cryptographically strong pseudo-random data
   13 
   14 exports.randomString = function (size) {
   15 
   16     var buffer = exports.randomBits((size + 1) * 6);
   17     if (buffer instanceof Error) {
   18         return buffer;
   19     }
   20 
   21     var string = buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
   22     return string.slice(0, size);
   23 };
   24 
   25 
   26 exports.randomBits = function (bits) {
   27 
   28     if (!bits ||
   29         bits < 0) {
   30 
   31         return Boom.internal('Invalid random bits count');
   32     }
   33 
   34     var bytes = Math.ceil(bits / 8);
   35     try {
   36         return Crypto.randomBytes(bytes);
   37     }
   38     catch (err) {
   39         return Boom.internal('Failed generating random bits: ' + err.message);
   40     }
   41 };
   42 
   43 
   44 // Compare two strings using fixed time algorithm (to prevent time-based analysis of MAC digest match)
   45 
   46 exports.fixedTimeComparison = function (a, b) {
   47 
   48     if (typeof a !== 'string' ||
   49         typeof b !== 'string') {
   50 
   51         return false;
   52     }
   53 
   54     var mismatch = (a.length === b.length ? 0 : 1);
   55     if (mismatch) {
   56         b = a;
   57     }
   58 
   59     for (var i = 0, il = a.length; i < il; ++i) {
   60         var ac = a.charCodeAt(i);
   61         var bc = b.charCodeAt(i);
   62         mismatch |= (ac ^ bc);
   63     }
   64 
   65     return (mismatch === 0);
   66 };
   67 
   68