Generate RFC4122 UUIDs and use in randStr

This commit is contained in:
Nils Maier 2013-07-31 22:07:50 +02:00
parent 993363d309
commit 1b7b8aa41c

View File

@ -1,5 +1,31 @@
angular.module('webui.services.utils', []) angular.module('webui.services.utils', [])
.factory('$utils', ['$filter', function(filter) { .factory('$utils', ['$filter', function(filter) {
var rnd16 = (function() {
"use strict";
var rndBuffer = Uint8Array(16);
var rnd16Weak = function() {
for (var i = 0, r; i < 16; i++) {
if (!(i % 0x3)) r = Math.random() * 0x100000000 | 0;
rndBuffer[i] = r >>> ((i & 0x3) << 0x3) & 0xff;
}
return rndBuffer;
};
if (!crypto.getRandomValues) {
return rnd16Weak;
}
return function() {
try {
crypto.getRandomValues(rndBuffer);
return rndBuffer;
}
catch (ex) {
// Entropy might be exhausted
return rnd16Weak();
}
};
})();
return { return {
// saves the key value pair in cookies // saves the key value pair in cookies
setCookie: function(key, value) { setCookie: function(key, value) {
@ -25,13 +51,26 @@ angular.module('webui.services.utils', [])
var seed = path.split(/[/\\]/); var seed = path.split(/[/\\]/);
return seed[seed.length - 1]; return seed[seed.length - 1];
}, },
randStr: function() { uuid: (function() {
var str = []; var bt = [];
var hexDigits = "0123456789abcdef"; for (var i = 0; i < 0x100; ++i) {
for (var i = 0; i < 36; i++) { bt.push((i + 0x100).toString(16).substr(1));
str[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
} }
return str.join(""); Object.freeze(bt);
return function() {
var r = rnd16();
r[6] = (r[6] & 0xf) | 0x40; // Version 4
r[8] = (r[8] & 0x3f) | 0x80; // Version 4y
return bt[r[0]] + bt[r[1]] + bt[r[2]] + bt[r[3]] + "-" +
bt[r[4]] + bt[r[5]] + "-" +
bt[r[6]] + bt[r[7]] + "-" +
bt[r[8]] + bt[r[9]] + "-" +
bt[r[10]] + bt[r[11]] + bt[r[12]] + bt[r[13]] + bt[r[14]] + bt[r[15]];
};
})(),
randStr: function() {
return this.uuid();
}, },
// maps the array in place to the destination // maps the array in place to the destination