Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
use window.crypto.getRandomValues
Browse files Browse the repository at this point in the history
  • Loading branch information
Diana Thayer committed Mar 16, 2017
1 parent 2bfddea commit 3983030
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 19 deletions.
47 changes: 41 additions & 6 deletions addon/webextension/makeUuid.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
window.makeUuid = (function () {

// From http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
// From http://pastebin.com/JSgC7eej
return function makeUuid() { // eslint-disable-line no-unused-vars
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
// get sixteen unsigned 8 bit random values
var u = window
.crypto
.getRandomValues(new Uint8Array(16));

// set the version bit to v4
u[6] = (u[6] & 0x0f) | 0x40

// set the variant bit to "don't care" (yes, the RFC
// calls it that)
u[8] = (u[8] & 0xbf) | 0x80

// hex encode them and add the dashes
var uid = "";
uid += u[0].toString(16);
uid += u[1].toString(16);
uid += u[2].toString(16);
uid += u[3].toString(16);
uid += "-";

uid += u[4].toString(16);
uid += u[5].toString(16);
uid += "-";

uid += u[6].toString(16);
uid += u[7].toString(16);
uid += "-";

uid += u[8].toString(16);
uid += u[9].toString(16);
uid += "-";

uid += u[10].toString(16);
uid += u[11].toString(16);
uid += u[12].toString(16);
uid += u[13].toString(16);
uid += u[14].toString(16);
uid += u[15].toString(16);

return uid;
};
})();
null;
54 changes: 41 additions & 13 deletions shared/shot.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,48 @@ function deepEqual(a, b) {
return true;
}

// From http://pastebin.com/JSgC7eej
function makeUuid() {
// FIXME: not a proper uuid
let id = "";
while (id.length < 12) {
// 46656 == Math.pow(36, 3)
let num;
if (! id) {
num = Date.now() % 46656;
} else {
num = Math.floor(Math.random() * 46656);
}
id += num.toString(36);
}
return id;
// get sixteen unsigned 8 bit random values
var u = window
.crypto
.getRandomValues(new Uint8Array(16));

// set the version bit to v4
u[6] = (u[6] & 0x0f) | 0x40

// set the variant bit to "don't care" (yes, the RFC
// calls it that)
u[8] = (u[8] & 0xbf) | 0x80

// hex encode them and add the dashes
var uid = "";
uid += u[0].toString(16);
uid += u[1].toString(16);
uid += u[2].toString(16);
uid += u[3].toString(16);
uid += "-";

uid += u[4].toString(16);
uid += u[5].toString(16);
uid += "-";

uid += u[6].toString(16);
uid += u[7].toString(16);
uid += "-";

uid += u[8].toString(16);
uid += u[9].toString(16);
uid += "-";

uid += u[10].toString(16);
uid += u[11].toString(16);
uid += u[12].toString(16);
uid += u[13].toString(16);
uid += u[14].toString(16);
uid += u[15].toString(16);

return uid;
}

class AbstractShot {
Expand Down

0 comments on commit 3983030

Please sign in to comment.