Add uuid generation functions that uses high quality RNG.

This commit is contained in:
Andrey Antukh 2016-04-24 21:49:14 +03:00
parent 5f86df81ec
commit 6e4d243897
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 116 additions and 0 deletions

47
vendor/uuid/impl.js vendored Normal file
View file

@ -0,0 +1,47 @@
/*
* 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.impl");
goog.require("uuid.rng");
goog.scope(function() {
const rng = uuid.rng;
const hexMap = [];
for (let i = 0; i < 256; i++) {
hexMap[i] = (i + 0x100).toString(16).substr(1);
}
function toHexString(buf) {
let i = 0;
return (hexMap[buf[i++]] +
hexMap[buf[i++]] +
hexMap[buf[i++]] +
hexMap[buf[i++]] + '-' +
hexMap[buf[i++]] +
hexMap[buf[i++]] + '-' +
hexMap[buf[i++]] +
hexMap[buf[i++]] + '-' +
hexMap[buf[i++]] +
hexMap[buf[i++]] + '-' +
hexMap[buf[i++]] +
hexMap[buf[i++]] +
hexMap[buf[i++]] +
hexMap[buf[i++]] +
hexMap[buf[i++]] +
hexMap[buf[i++]]);
}
uuid.impl.v4 = function() {
const buf = rng.getBytes(16);
buf[6] = (buf[6] & 0x0f) | 0x40;
buf[8] = (buf[8] & 0x3f) | 0x80;
return cljs.core.uuid(toHexString(buf));
};
})