Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: reseed and extraSeed #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions uuid.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export interface UUIDConstructor {
/* making construction */
new(version: number): UUID;
new(version: number, ns: string, data: string): UUID;

/* reseed the internal pseudo random number generator */
reseed(seed: string);

/* provide extra entropy to the internal pseudo random number generator */
extraSeed(extraSeed: string);
}

declare var UUID: UUIDConstructor;
Expand Down
20 changes: 20 additions & 0 deletions uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,26 @@

UUID.PCG = PCG;

UUID.extraSeed = function (extraSeed) {
if (typeof extraSeed !== "string") {
throw new Error("UUID: extraSeed: invalid argument (string expected)");
}
var expanded = sha1_core(
s2a(extraSeed, { ibits: 8, obits: 32, obigendian: true }),
extraSeed.length * 8)
for (var i = 0; i < expanded.length; i++) {
ui64_xor(pcg.state, ui64_n2i((expanded[i] >>> 0)));
}
}

UUID.reseed = function (seed) {
if (typeof seed !== "string") {
throw new Error("UUID: reseed: invalid argument (string expected)");
}
pcg = new PCG(0);
UUID.extraSeed(seed);
}

/* export API */
return UUID;
}));
Expand Down
45 changes: 45 additions & 0 deletions uuid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,50 @@ describe("UUID base functionality", function () {
expect(function () { new UUID().parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); })
.to.throw(Error);
});
it("reseed and extraSeed should work reliably", function () {
var uuid0, uuid1, uuid2, uuid3, uuid4;

UUID.reseed("seed0");
expect(new UUID(4).format())
.to.be.equal(uuid0 = "f8a86041-ca57-4c76-b48a-6dedf4b9acf4");
expect(new UUID(4).format())
.to.be.equal(uuid1 = "704a0f12-1376-4ef0-b6e1-9d57e5a8a492")
.to.not.be.equal(uuid0);
UUID.reseed("seed1");
expect(new UUID(4).format())
.to.be.equal(uuid1 = "31e91c7c-8e7c-4afc-8aa8-c7e42a48b205")
.to.not.be.equal(uuid0);
UUID.reseed("seed0");
expect(new UUID(4).format()).to.be.equal(uuid0);

UUID.reseed("seed0");
expect(new UUID(4).format()).to.be.equal(uuid0);
UUID.extraSeed("seed1");
expect(new UUID(4).format())
.to.be.equal(uuid2 = "dfbf1436-95c6-472a-bc22-ab07219721b4")
.to.not.be.equal(uuid1);
UUID.extraSeed("seed2");
expect(new UUID(4).format())
.to.be.equal(uuid3 = "03f775f7-cd94-4896-aefa-edac6b508025")
.to.not.be.equal(uuid1)
.to.not.be.equal(uuid2);
UUID.reseed("seed0");
expect(new UUID(4).format()).to.be.equal(uuid0);

UUID.reseed("seed1");
expect(new UUID(4).format()).to.be.equal(uuid1);
expect(new UUID(4).format())
.to.be.equal("4d01aa6c-0fdd-48d7-b118-38123215d703")
.to.not.be.equal(uuid2);

UUID.reseed("seed2");
expect(new UUID(4).format())
.to.be.equal(uuid4 = "ec8a811e-8d27-433e-8ba9-b3a4b199cd2e")
.to.not.be.equal(uuid3);
expect(new UUID(4).format())
.to.be.equal("d41053e3-447a-49ca-94f5-240987c0a9c2")
.to.not.be.equal(uuid3)
.to.not.be.equal(uuid4)
})
});