Create UUID - Node.js Security

Node.js examples for Security:GUID

Description

Create UUID

Demo Code


// UUID code taken from http://lists.evolt.org/pipermail/javascript/2006-July/010716.html
UUID = {//from w  ww.j  av  a2  s  . co  m
    generate: function (type) {
  switch ((type || 'v4').toUpperCase ()) {
      // Version 4 UUID  (Section 4.4 of RFC 4122)
      case 'V4':
          var tl    = this._randomHexString (8);                  
// time_low
          var tm    = this._randomHexString (4);                  
// time_mid
          var thav  = '4' + this._randomHexString (3);            
// time_hi_and_version
          var cshar = Math.randomInt (0, 0xFF);                   
// clock_seq_hi_and_reserved
              cshar = ((cshar & ~(1 << 6)) | (1 << 7)).toString (16);
          var csl   = this._randomHexString (2);                  
// clock_seq_low
          var n     = this._randomHexString (12);                 
// node

          return (tl + '-' + tm + '-' + thav + '-' + cshar + csl + 
'-' + n);

      // Nil UUID  (Section 4.1.7 of RFC 4122)
      case 'NIL':
          return ('00000000-0000-0000-0000-000000000000');
  }
  return (null);
    },

    _randomHexString: function (len) {
  var random = Math.randomInt (0, Math.pow (16, len) - 1);
  return (random.toString (16).pad (len, '0', String.PAD_LEFT));
    }
}

Related Tutorials