mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-08 15:25:01 -04:00
ref generate uuid versions v1, v3, v4 and v5
This commit is contained in:
parent
cdcec4dd03
commit
9d0bd40979
1 changed files with 30 additions and 14 deletions
|
@ -5,8 +5,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import crypto from "crypto";
|
import * as uuid from "uuid";
|
||||||
|
import OperationError from "../errors/OperationError.mjs";
|
||||||
/**
|
/**
|
||||||
* Generate UUID operation
|
* Generate UUID operation
|
||||||
*/
|
*/
|
||||||
|
@ -20,11 +20,24 @@ class GenerateUUID extends Operation {
|
||||||
|
|
||||||
this.name = "Generate UUID";
|
this.name = "Generate UUID";
|
||||||
this.module = "Crypto";
|
this.module = "Crypto";
|
||||||
this.description = "Generates an RFC 4122 version 4 compliant Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).<br><br>A version 4 UUID relies on random numbers, in this case generated using <code>window.crypto</code> if available and falling back to <code>Math.random</code> if not.";
|
this.description = "Generates an RFC 4122 compliant Universally Unique Identifier (UUID), also known as a Globally Unique Identifier (GUID).<br><br>A version 4 UUID relies on random numbers, in this case generated using <code>uuid</code> package";
|
||||||
this.infoURL = "https://wikipedia.org/wiki/Universally_unique_identifier";
|
this.infoURL = "https://wikipedia.org/wiki/Universally_unique_identifier";
|
||||||
this.inputType = "string";
|
this.inputType = "string";
|
||||||
this.outputType = "string";
|
this.outputType = "string";
|
||||||
this.args = [];
|
this.args = [
|
||||||
|
{
|
||||||
|
name: "UUID Version",
|
||||||
|
type: "option",
|
||||||
|
value: [
|
||||||
|
"v1", "v3", "v4", "v5"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "UUID namespace (valid for v3 and v5)",
|
||||||
|
type: "string",
|
||||||
|
value: "1b671a64-40d5-491e-99b0-da01ff1f3341"
|
||||||
|
}
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,16 +46,19 @@ class GenerateUUID extends Operation {
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const buf = new Uint32Array(4).map(() => {
|
const [version, namespace] = args;
|
||||||
return crypto.randomBytes(4).readUInt32BE(0, true);
|
const hasDesiredVersion = typeof uuid[version] === "function";
|
||||||
});
|
if (!hasDesiredVersion) throw new OperationError("Invalid UUID version");
|
||||||
let i = 0;
|
|
||||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
const versionThatRequiresNamespace = ["v3", "v5"];
|
||||||
const r = (buf[i >> 3] >> ((i % 8) * 4)) & 0xf,
|
|
||||||
v = c === "x" ? r : (r & 0x3 | 0x8);
|
const requiresNamespace = versionThatRequiresNamespace.includes(version);
|
||||||
i++;
|
if (!requiresNamespace) return uuid[version]();
|
||||||
return v.toString(16);
|
|
||||||
});
|
const hasValidNamespace = typeof namespace === "string" && uuid.validate(namespace);
|
||||||
|
if (!hasValidNamespace) throw new OperationError("Invalid UUID namespace");
|
||||||
|
|
||||||
|
return uuid[version](input, namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue