Advertisement
AyrA

bignum fuckup fix in mongoose-shortid

Dec 10th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = exports = function() {
  2.  
  3.     var bignum = require('bignum');
  4.     var crypto = require('crypto');
  5.  
  6.  
  7.     /*
  8.      * Default encoding alphabets
  9.      * URL-friendly base-64 encoding is chosen.  Base-32 is best suited
  10.      * for tiny-URL like applications, because I and 1 can't be confused
  11.      * and the upper-case characters are more easily remembered by a human.
  12.      *
  13.      * Where "native", we use the bignum native encoding routine.
  14.      */
  15.     var defaultAlphabets = {
  16.         10: "0123456789",
  17.         16: "0123456789ABCDEF",
  18.         32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
  19.         36: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  20.         62: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
  21.         64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
  22.     };
  23.  
  24.     /*
  25.     // Convert the bignum to a string with the given base
  26.     function bignumToString(bignum, base, alphabet) {
  27.         // Prefer native conversion
  28.         if (alphabet === "native" && base != 6) {
  29.             return bignum.toString(base);
  30.         }
  31.  
  32.         // Old-sk00l conversion
  33.         var result = [];
  34.  
  35.         while (bignum.gt(0)) {
  36.             var ord = bignum.mod(base);
  37.             result.push(alphabet.charAt(ord));
  38.             bignum = bignum.div(base);
  39.         }
  40.  
  41.         return result.reverse().join("");
  42.     }
  43.     */
  44.  
  45.     return function(options, cb) {
  46.         var len = options.len || 7;
  47.         // if an alphabet was specified it takes priorty
  48.         // otherwise use the default alphabet for the given base
  49.         var base = options.alphabet ? options.alphabet.length : (options.base || 64);
  50.         var alphabet = options.alphabet || defaultAlphabets[base];
  51.  
  52.         if (!alphabet) {
  53.             var err = new Error(
  54.                 "Only base " +
  55.                 Object.keys(alphabets).join(", ") +
  56.                 " supported if an alphabet is not provided."
  57.             );
  58.             cb(err, null);
  59.             return;
  60.         }
  61.  
  62.         /*
  63.         // Generate a random byte string of the required length
  64.         var bytes = Math.floor( len * Math.log(base) / Math.log(256) );
  65.         crypto.pseudoRandomBytes(bytes, function(err, buf) {
  66.  
  67.             // Propagate errors...
  68.             if (err) {
  69.                 cb(err, null);
  70.                 return;
  71.             }
  72.            
  73.             var num=0;
  74.             var str="";
  75.             for(var i=0;i<buf.length;i++)
  76.             {
  77.                 num=num<<8;
  78.                 num+=buf[i];
  79.                 if(i>0 && i%6==0)
  80.                 {
  81.                     str+=num.toString();
  82.                     num=0;
  83.                 }
  84.             }
  85.             str+=num.toString();
  86.            
  87.             // Convert to the required base
  88.             //var num = bignum.fromBuffer(buf);
  89.             var id = str.toString();//bignumToString(num, base, alphabet);
  90.  
  91.             // Prefix with the first char to reach the desired fixed length string
  92.             id = Array(len - id.length + 1).join(alphabet[0]) + id;
  93.            
  94.             cb(null, id);
  95.         });
  96.         */
  97.         /*
  98.             TEMP: Since bignum fucked up and we only need it for number generation,
  99.             this does the job too.
  100.             - Kevin Gut [11.06.2015 11:06]
  101.         */
  102.         var id="";
  103.         while(id.length<len)
  104.         {
  105.             id+=alphabet[(Math.random()*alphabet.length|0)].toString();
  106.         }
  107.        
  108.         cb(null, id);
  109.     };
  110. }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement