Use ISAAC
This commit is contained in:
parent
e9772338df
commit
82f5319c96
3 changed files with 15 additions and 8 deletions
|
|
@ -18,8 +18,8 @@ proc `==`*(x, y: UUID): bool
|
||||||
|
|
||||||
proc genUUID*(): UUID =
|
proc genUUID*(): UUID =
|
||||||
## Returns a random (v4) UUID.
|
## Returns a random (v4) UUID.
|
||||||
## Uses random values obtained from system source (e.g. urandom).
|
## Uses a thread-local cryptographically secure PRNG (ISAAC) seeded with
|
||||||
## In the future this will use a cryptographically secure PRNG for efficiency.
|
## true random values obtained from OS.
|
||||||
|
|
||||||
proc parseUUID*(s: string): UUID {.raises: [ValueError].}
|
proc parseUUID*(s: string): UUID {.raises: [ValueError].}
|
||||||
## Converts string representation of an UUID to UUID object.
|
## Converts string representation of an UUID to UUID object.
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import strutils, hashes
|
import strutils, hashes
|
||||||
|
import isaac
|
||||||
import urandom
|
import urandom
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -39,13 +40,16 @@ proc `==`*(x, y: UUID): bool =
|
||||||
## Returns ``true`` when the specified UUIDs are equal, ``false`` otherwise.
|
## Returns ``true`` when the specified UUIDs are equal, ``false`` otherwise.
|
||||||
x.mostSigBits == y.mostSigBits and x.leastSigBits == y.leastSigBits
|
x.mostSigBits == y.mostSigBits and x.leastSigBits == y.leastSigBits
|
||||||
|
|
||||||
|
var rand {.threadvar.}: IsaacGenerator
|
||||||
proc genUUID*(): UUID =
|
proc genUUID*(): UUID =
|
||||||
## Returns a random (v4) UUID.
|
## Returns a random (v4) UUID.
|
||||||
## Uses random values obtained from system source (e.g. urandom).
|
## Uses a thread-local cryptographically secure PRNG (ISAAC) seeded with
|
||||||
## In the future this will use a cryptographically secure PRNG for efficiency.
|
## true random values obtained from OS.
|
||||||
var randBytes: array[16, uint8] = urandom(16)
|
if rand == nil:
|
||||||
copyMem(addr result.mostSigBits, addr randBytes[0], 8)
|
var seed = cast[array[256, uint32]](urandom(1024))
|
||||||
copyMem(addr result.leastSigBits, addr randBytes[8], 8)
|
rand = newIsaacGenerator(seed)
|
||||||
|
result.mostSigBits = cast[int64]((rand.nextU32().uint64 shl 32) or rand.nextU32())
|
||||||
|
result.leastSigBits = cast[int64]((rand.nextU32().uint64 shl 32) or rand.nextU32())
|
||||||
|
|
||||||
# set version to 4
|
# set version to 4
|
||||||
result.mostSigBits = (result.mostSigBits and 0xFFFFFFFFFFFF0FFF'i64) or
|
result.mostSigBits = (result.mostSigBits and 0xFFFFFFFFFFFF0FFF'i64) or
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
[Package]
|
[Package]
|
||||||
name: "uuids"
|
name: "uuids"
|
||||||
version: "0.1.0"
|
version: "0.1.1"
|
||||||
author: "Xored Software, Inc."
|
author: "Xored Software, Inc."
|
||||||
description: "UUID library"
|
description: "UUID library"
|
||||||
license: "MIT"
|
license: "MIT"
|
||||||
srcDir: "src"
|
srcDir: "src"
|
||||||
|
|
||||||
|
[Deps]
|
||||||
|
requires: "isaac >= 0.1.0"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue