19 lines
470 B
JavaScript
19 lines
470 B
JavaScript
const crypto = require("crypto");
|
|
|
|
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
function valid(uuid) {
|
|
return uuidPattern.test(uuid);
|
|
}
|
|
|
|
function uuid4() {
|
|
var rnd = crypto.randomBytes(16);
|
|
rnd[6] = (rnd[6] & 0x0f) | 0x40;
|
|
rnd[8] = (rnd[8] & 0x3f) | 0x80;
|
|
rnd = rnd.toString("hex").match(/(.{8})(.{4})(.{4})(.{4})(.{12})/);
|
|
rnd.shift();
|
|
return rnd.join("-");
|
|
}
|
|
uuid4.valid = valid;
|
|
|
|
module.exports = uuid4;
|