Skip to content

Commit 80579ca

Browse files
committed
Use logic from node-uuid v1.4.7
1 parent adda90a commit 80579ca

File tree

2 files changed

+57
-60
lines changed

2 files changed

+57
-60
lines changed

angular-uuid.js

+56-59
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,34 @@
44
// This is an AngularJS wrapper for the original node-uuid library
55
// written by Robert Kieffer – https://github.com/broofa/node-uuid
66
// MIT License - http://opensource.org/licenses/mit-license.php
7+
// The wrapped node-uuid library is at version 1.4.7
78

8-
function AngularUUID ()
9-
{
10-
angular.module("angular-uuid",[]).factory("uuid", ["$window", uuid]);
9+
function AngularUUID () {
10+
'use strict';
1111

12-
function uuid ($window)
13-
{
14-
var _global = $window;
12+
angular.module('angular-uuid',[]).factory('uuid', ['$window', nodeUUID]);
1513

14+
function nodeUUID ($window) {
1615
// Unique ID creation requires a high quality random # generator. We feature
1716
// detect to determine the best RNG source, normalizing to a function that
1817
// returns 128-bits of randomness, since that's what's usually required
19-
var _rng;
18+
var _rng, _mathRNG, _whatwgRNG;
2019

21-
// Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html
22-
//
23-
// Moderately fast, high quality
24-
if (typeof(_global.require) == 'function') {
25-
try {
26-
var _rb = _global.require('crypto').randomBytes;
27-
_rng = _rb && function() {return _rb(16);};
28-
} catch(e) {}
29-
}
20+
// Allow for MSIE11 msCrypto
21+
var _crypto = $window.crypto || $window.msCrypto;
3022

31-
if (!_rng && _global.crypto && crypto.getRandomValues) {
23+
if (!_rng && _crypto && _crypto.getRandomValues) {
3224
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
3325
//
3426
// Moderately fast, high quality
35-
var _rnds8 = new Uint8Array(16);
36-
_rng = function whatwgRNG() {
37-
crypto.getRandomValues(_rnds8);
38-
return _rnds8;
39-
};
27+
try {
28+
var _rnds8 = new Uint8Array(16);
29+
_whatwgRNG = _rng = function whatwgRNG() {
30+
_crypto.getRandomValues(_rnds8);
31+
return _rnds8;
32+
};
33+
_rng();
34+
} catch(e) {}
4035
}
4136

4237
if (!_rng) {
@@ -45,18 +40,21 @@ function AngularUUID ()
4540
// If all else fails, use Math.random(). It's fast, but is of unspecified
4641
// quality.
4742
var _rnds = new Array(16);
48-
_rng = function() {
43+
_mathRNG = _rng = function() {
4944
for (var i = 0, r; i < 16; i++) {
50-
if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
45+
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
5146
_rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
5247
}
5348

5449
return _rnds;
5550
};
51+
if ('undefined' !== typeof console && console.warn) {
52+
console.warn('[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()');
53+
}
5654
}
5755

5856
// Buffer class to use
59-
var BufferClass = typeof(_global.Buffer) == 'function' ? _global.Buffer : Array;
57+
var BufferClass = ('function' === typeof Buffer) ? Buffer : Array;
6058

6159
// Maps for number <-> hex string conversion
6260
var _byteToHex = [];
@@ -74,8 +72,8 @@ function AngularUUID ()
7472
s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
7573
if (ii < 16) { // Don't overflow!
7674
buf[i + ii++] = _hexToByte[oct];
77-
}
78-
});
75+
}
76+
});
7977

8078
// Zero out remaining bytes if string was short
8179
while (ii < 16) {
@@ -89,13 +87,13 @@ function AngularUUID ()
8987
function unparse(buf, offset) {
9088
var i = offset || 0, bth = _byteToHex;
9189
return bth[buf[i++]] + bth[buf[i++]] +
92-
bth[buf[i++]] + bth[buf[i++]] + '-' +
93-
bth[buf[i++]] + bth[buf[i++]] + '-' +
94-
bth[buf[i++]] + bth[buf[i++]] + '-' +
95-
bth[buf[i++]] + bth[buf[i++]] + '-' +
96-
bth[buf[i++]] + bth[buf[i++]] +
97-
bth[buf[i++]] + bth[buf[i++]] +
98-
bth[buf[i++]] + bth[buf[i++]];
90+
bth[buf[i++]] + bth[buf[i++]] + '-' +
91+
bth[buf[i++]] + bth[buf[i++]] + '-' +
92+
bth[buf[i++]] + bth[buf[i++]] + '-' +
93+
bth[buf[i++]] + bth[buf[i++]] + '-' +
94+
bth[buf[i++]] + bth[buf[i++]] +
95+
bth[buf[i++]] + bth[buf[i++]] +
96+
bth[buf[i++]] + bth[buf[i++]];
9997
}
10098

10199
// **`v1()` - Generate time-based UUID**
@@ -108,8 +106,8 @@ function AngularUUID ()
108106

109107
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
110108
var _nodeId = [
111-
_seedBytes[0] | 0x01,
112-
_seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
109+
_seedBytes[0] | 0x01,
110+
_seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
113111
];
114112

115113
// Per 4.2.2, randomize (14 bit) clockseq
@@ -125,17 +123,17 @@ function AngularUUID ()
125123

126124
options = options || {};
127125

128-
var clockseq = options.clockseq != null ? options.clockseq : _clockseq;
126+
var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq;
129127

130128
// UUID timestamps are 100 nano-second units since the Gregorian epoch,
131129
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
132130
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
133131
// (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
134-
var msecs = options.msecs != null ? options.msecs : new Date().getTime();
132+
var msecs = (options.msecs != null) ? options.msecs : new Date().getTime();
135133

136134
// Per 4.2.1.2, use count of uuid's generated during the current clock
137135
// cycle to simulate higher resolution clock
138-
var nsecs = options.nsecs != null ? options.nsecs : _lastNSecs + 1;
136+
var nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1;
139137

140138
// Time since last uuid creation (in msecs)
141139
var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
@@ -201,8 +199,8 @@ function AngularUUID ()
201199
// Deprecated - 'format' argument, as supported in v1.2
202200
var i = buf && offset || 0;
203201

204-
if (typeof(options) == 'string') {
205-
buf = options == 'binary' ? new BufferClass(16) : null;
202+
if (typeof(options) === 'string') {
203+
buf = (options === 'binary') ? new BufferClass(16) : null;
206204
options = null;
207205
}
208206
options = options || {};
@@ -224,31 +222,30 @@ function AngularUUID ()
224222
}
225223

226224
// Export public API
227-
var publicAPI = v4;
228-
publicAPI.v1 = v1;
229-
publicAPI.v4 = v4;
230-
publicAPI.parse = parse;
231-
publicAPI.unparse = unparse;
232-
publicAPI.BufferClass = BufferClass;
233-
234-
return publicAPI;
225+
var uuid = v4;
226+
uuid.v1 = v1;
227+
uuid.v4 = v4;
228+
uuid.parse = parse;
229+
uuid.unparse = unparse;
230+
uuid.BufferClass = BufferClass;
231+
uuid._rng = _rng;
232+
uuid._mathRNG = _mathRNG;
233+
uuid._whatwgRNG = _whatwgRNG;
234+
235+
return uuid;
235236
}
236237
}
237238

238239
// check for Module/AMD support, otherwise call the uuid function to setup the angular module.
239-
if (typeof module !== "undefined" && module.exports)
240-
{
240+
if (typeof module !== 'undefined' && module.exports) {
241241
module.exports = new AngularUUID();
242-
}
243-
else if (typeof define !== "undefined" && define.amd)
244-
{
242+
243+
} else if (typeof define !== 'undefined' && define.amd) {
245244
// AMD. Register as an anonymous module.
246-
define (function()
247-
{
245+
define (function() {
248246
return new AngularUUID();
249247
});
250-
}
251-
else
252-
{
248+
249+
} else {
253250
AngularUUID();
254251
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "angular-uuid",
33
"title": "angular-uuid",
44
"description": "An AngularJS wrapper for a rigorous implementation of RFC4122 (v1 and v4) UUIDs.",
5-
"version": "0.0.3",
5+
"version": "0.0.4",
66
"homepage": "https://github.com/munkychop/angular-uuid",
77
"author": {
88
"name": "Ivan Hayes",

0 commit comments

Comments
 (0)