mirror of
https://github.com/penpot/penpot.git
synced 2025-05-22 04:46:10 +02:00
Add uuid generation functions that uses high quality RNG.
This commit is contained in:
parent
5f86df81ec
commit
6e4d243897
3 changed files with 116 additions and 0 deletions
44
vendor/uuid/rng.js
vendored
Normal file
44
vendor/uuid/rng.js
vendored
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
goog.provide("uuid.rng");
|
||||
|
||||
goog.scope(function() {
|
||||
uuid.rng.getBytes = null;
|
||||
|
||||
// Check if nodejs rng is available (high quality);
|
||||
if (goog.global.require !== undefined) {
|
||||
const crypto = goog.global.require("crypto");
|
||||
|
||||
uuid.rng.getBytes = function(n) {
|
||||
return crypto.randomBytes(n);
|
||||
};
|
||||
}
|
||||
// Check if whatwg rng is available (high quality);
|
||||
else if (goog.global.crypto.getRandomValues !== undefined) {
|
||||
uuid.rng.getBytes = function(n) {
|
||||
const buf = new Uint8Array(16);
|
||||
goog.global.crypto.getRandomValues(buf);
|
||||
return buf;
|
||||
};
|
||||
}
|
||||
// Switch Back to the Math.random (low quality);
|
||||
else {
|
||||
console.warn("No high quality RNG available, switching back to Math.random.");
|
||||
uuid.rng.getBytes = function(n) {
|
||||
const buf = new Array(16);
|
||||
for (let i = 0, r; i < 16; i++) {
|
||||
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
|
||||
buf[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
||||
}
|
||||
return buf;
|
||||
};
|
||||
}
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue