From ff5d895688640dd54fe2964c30fb4ff2376f7346 Mon Sep 17 00:00:00 2001 From: Anatoly Galiulin Date: Mon, 13 Mar 2017 13:44:41 +0700 Subject: [PATCH 1/8] Fix ``isaac`` dependency --- uuids.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uuids.nimble b/uuids.nimble index 1e2d425..57cd53a 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -7,4 +7,4 @@ license: "MIT" srcDir: "src" [Deps] -requires: "isaac >= 0.1.0" +requires: "isaac >= 0.1.2" From 654e32c43e0681afc6171ac8c1578f8938e5cf86 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Mon, 13 Mar 2017 13:52:15 +0700 Subject: [PATCH 2/8] Up version to 0.1.5 --- uuids.nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uuids.nimble b/uuids.nimble index 57cd53a..b7d3c60 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -1,6 +1,6 @@ [Package] name: "uuids" -version: "0.1.4" +version: "0.1.5" author: "Xored Software, Inc." description: "UUID library" license: "MIT" From c58d9474eedafff09807edef9db50639b5663dd6 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Sun, 18 Jun 2017 22:24:57 +0700 Subject: [PATCH 3/8] Implement own parseHexInt stdlib version allows some non-hex characters and doesn't work with long numbers on 32-bit platforms --- src/uuids.nim | 40 ++++++++++++++++++++++++++++------------ uuids.nimble | 2 +- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/uuids.nim b/src/uuids.nim index 6175e44..4d04727 100644 --- a/src/uuids.nim +++ b/src/uuids.nim @@ -18,6 +18,21 @@ template toHex(s: string, start: Natural, # handle negative overflow if n == 0 and x < 0: n = -1 +proc uuidsParseHexInt(s: string, maxLen: int): int64 = + if s.isNil or s.len == 0: + raise newException(ValueError, "UUID part is empty") + if s.len > maxLen or s.len > sizeof(result) * 2: + raise newException(ValueError, "UUID part is longer than expected") + for c in s: + case c + of '0'..'9': + result = result shl 4 or (ord(c) - ord('0')) + of 'a'..'f': + result = result shl 4 or (ord(c) - ord('a') + 10) + of 'A'..'F': + result = result shl 4 or (ord(c) - ord('A') + 10) + else: raise newException(ValueError, "Invalid hex string: " & s) + proc `$`*(uuid: UUID): string = ## Returns a string representation of the UUID in canonical form. result = newString(36) @@ -69,25 +84,26 @@ proc parseUUID*(s: string): UUID {.raises: [ValueError].} = if parts.len != 5: raise newException(ValueError, "UUID must consist of 5 parts separated with `-`") - var mostSigBits: int64 = parseHexInt(parts[0]) + var mostSigBits: int64 = uuidsParseHexInt(parts[0], 8) mostSigBits = mostSigBits shl 16 - mostSigBits = mostSigBits or parseHexInt(parts[1]) + mostSigBits = mostSigBits or uuidsParseHexInt(parts[1], 4) mostSigBits = mostSigBits shl 16 - mostSigBits = mostSigBits or parseHexInt(parts[2]) + mostSigBits = mostSigBits or uuidsParseHexInt(parts[2], 4) - var leastSigBits: int64 = parseHexInt(parts[3]) + var leastSigBits: int64 = uuidsParseHexInt(parts[3], 4) leastSigBits = leastSigBits shl 48 - leastSigBits = leastSigBits or parseHexInt(parts[4]) + leastSigBits = leastSigBits or uuidsParseHexInt(parts[4], 12) result = UUID(mostSigBits: mostSigBits, leastSigBits: leastSigBits) when isMainModule: var uuid: UUID assert(uuid.isZero()) - uuid = genUUID() - let uuidStr = $uuid - assert(uuidStr.len == 36) - assert(uuidStr[14] == '4') # version - assert(uuidStr[19] in {'8', '9', 'a', 'b'}) # variant (2 bits) - assert(uuidStr.parseUUID() == uuid) - assert(uuidStr.parseUUID().hash() == uuid.hash()) + for i in 1..100: + uuid = genUUID() + let uuidStr = $uuid + assert(uuidStr.len == 36) + assert(uuidStr[14] == '4') # version + assert(uuidStr[19] in {'8', '9', 'a', 'b'}) # variant (2 bits) + assert(uuidStr.parseUUID() == uuid) + assert(uuidStr.parseUUID().hash() == uuid.hash()) diff --git a/uuids.nimble b/uuids.nimble index b7d3c60..7e45adf 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -1,6 +1,6 @@ [Package] name: "uuids" -version: "0.1.5" +version: "0.1.6" author: "Xored Software, Inc." description: "UUID library" license: "MIT" From 78fadcb469db41b48a87c3eac879961196963529 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Sun, 18 Jun 2017 22:37:13 +0700 Subject: [PATCH 4/8] Expose mostSigBits and leastSigBits --- README.md | 17 +++++++++++++---- src/uuids.nim | 33 ++++++++++++++++++++++++++++----- uuids.nimble | 2 +- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2709427..fd64a5a 100644 --- a/README.md +++ b/README.md @@ -7,19 +7,28 @@ API: type UUID* = object ## 128-bit UUID compliant with RFC-4122 +proc initUUID*(mostSigBits, leastSigBits: int64): UUID = + ## Initializes UUID with the specified most and least significant bits + +proc leastSigBits*(uuid: UUID): int64 {.inline.} + ## Returns 64 least significant bits of the ``uuid`` + +proc mostSigBits*(uuid: UUID): int64 {.inline.} + ## Returns 64 most significant bits of the ``uuid`` + proc `$`*(uuid: UUID): string - ## Returns a string representation of the UUID in canonical form. + ## Returns a string representation of the ``uuid`` in canonical form. proc hash*(uuid: UUID): Hash - ## Computes hash of the specified UUID. + ## Computes hash of the specified ``uuid``. proc `==`*(x, y: UUID): bool ## Returns true when the specified UUIDs are equal, false otherwise. proc isZero*(uuid: UUID): bool - ## Returns ``true`` when the UUID is zero (not set), ``false`` otherwise. + ## Returns ``true`` when the ``uuid`` is zero (not set), ``false`` otherwise. -proc genUUID*(): UUID = +proc genUUID*(): UUID ## Returns a random (v4) UUID. ## Uses a thread-local cryptographically secure PRNG (ISAAC) seeded with ## true random values obtained from OS. diff --git a/src/uuids.nim b/src/uuids.nim index 4d04727..4a1f034 100644 --- a/src/uuids.nim +++ b/src/uuids.nim @@ -33,8 +33,21 @@ proc uuidsParseHexInt(s: string, maxLen: int): int64 = result = result shl 4 or (ord(c) - ord('A') + 10) else: raise newException(ValueError, "Invalid hex string: " & s) +proc initUUID*(mostSigBits, leastSigBits: int64): UUID = + ## Initializes UUID with the specified most and least significant bits + result.mostSigBits = mostSigBits + result.leastSigBits = leastSigBits + +proc leastSigBits*(uuid: UUID): int64 {.inline.} = + ## Returns 64 least significant bits of the ``uuid`` + uuid.leastSigBits + +proc mostSigBits*(uuid: UUID): int64 {.inline.} = + ## Returns 64 most significant bits of the ``uuid`` + uuid.mostSigBits + proc `$`*(uuid: UUID): string = - ## Returns a string representation of the UUID in canonical form. + ## Returns a string representation of the ``uuid`` in canonical form. result = newString(36) toHex(result, 0, uuid.mostSigBits shr 32, 8) result[8] = '-' @@ -47,7 +60,7 @@ proc `$`*(uuid: UUID): string = toHex(result, 24, uuid.leastSigBits, 12) proc hash*(uuid: UUID): Hash = - ## Computes hash of the specified UUID. + ## Computes hash of the specified ``uuid``. result = uuid.mostSigBits.hash() !& uuid.leastSigBits.hash() result = !$result @@ -56,7 +69,7 @@ proc `==`*(x, y: UUID): bool = x.mostSigBits == y.mostSigBits and x.leastSigBits == y.leastSigBits proc isZero*(uuid: UUID): bool = - ## Returns ``true`` when the UUID is zero (not set), ``false`` otherwise. + ## Returns ``true`` when the ``uuid`` is zero (not set), ``false`` otherwise. uuid.mostSigBits == 0'i64 and uuid.leastSigBits == 0'i64 var rand {.threadvar.}: IsaacGenerator @@ -105,5 +118,15 @@ when isMainModule: assert(uuidStr.len == 36) assert(uuidStr[14] == '4') # version assert(uuidStr[19] in {'8', '9', 'a', 'b'}) # variant (2 bits) - assert(uuidStr.parseUUID() == uuid) - assert(uuidStr.parseUUID().hash() == uuid.hash()) + + let parsedUUID = uuidStr.parseUUID() + assert(parsedUUID == uuid) + assert(parsedUUID.hash() == uuid.hash()) + assert(mostSigBits(parsedUUID) == mostSigBits(uuid)) + assert(leastSigBits(parsedUUID) == leastSigBits(uuid)) + + let newUUID = initUUID(mostSigBits(uuid), leastSigBits(uuid)) + assert(newUUID == uuid) + assert(newUUID.hash() == uuid.hash()) + assert(mostSigBits(newUUID) == mostSigBits(uuid)) + assert(leastSigBits(newUUID) == leastSigBits(uuid)) diff --git a/uuids.nimble b/uuids.nimble index 7e45adf..7a5c79a 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -1,6 +1,6 @@ [Package] name: "uuids" -version: "0.1.6" +version: "0.1.7" author: "Xored Software, Inc." description: "UUID library" license: "MIT" From 57396065ce1a3990a91100e528cfcb99bdac9b4c Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Wed, 1 Nov 2017 19:53:09 +0100 Subject: [PATCH 5/8] Fix for latest Nim --- src/uuids.nim | 5 +++-- uuids.nimble | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/uuids.nim b/src/uuids.nim index 4a1f034..3127c31 100644 --- a/src/uuids.nim +++ b/src/uuids.nim @@ -10,10 +10,11 @@ type template toHex(s: string, start: Natural, x: BiggestInt, len: Positive) = - const HexChars = "0123456789abcdef" + const HexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', + '9', 'a', 'b', 'c', 'd', 'e', 'f'] var n = x for j in countdown(len - 1, 0): - s[start + j] = HexChars[n and 0xF] + s[start + j] = HexChars[int(n and 0xF)] n = n shr 4 # handle negative overflow if n == 0 and x < 0: n = -1 diff --git a/uuids.nimble b/uuids.nimble index 7a5c79a..a670f52 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -1,6 +1,6 @@ [Package] name: "uuids" -version: "0.1.7" +version: "0.1.8" author: "Xored Software, Inc." description: "UUID library" license: "MIT" From 2ff415fb2b020f01149f3292bdb0c7e7e0e068d0 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Wed, 22 Nov 2017 16:53:53 +0700 Subject: [PATCH 6/8] Update isaac dependency --- uuids.nimble | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uuids.nimble b/uuids.nimble index a670f52..3fcc1fd 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -1,10 +1,10 @@ [Package] name: "uuids" -version: "0.1.8" +version: "0.1.9" author: "Xored Software, Inc." description: "UUID library" license: "MIT" srcDir: "src" [Deps] -requires: "isaac >= 0.1.2" +requires: "isaac >= 0.1.3" From c5039c1cc6a8a93fc2f3c03a206372eb4412e63b Mon Sep 17 00:00:00 2001 From: Giovanni Date: Wed, 5 Sep 2018 15:22:45 +0900 Subject: [PATCH 7/8] fixed nimble check and compatibility with new nim compiler (strings can't be nil) --- .gitignore | 2 -- src/uuids.nim => uuids.nim | 4 ++-- uuids.nimble | 3 +-- {src => uuids}/urandom.nim | 0 4 files changed, 3 insertions(+), 6 deletions(-) rename src/uuids.nim => uuids.nim (99%) rename {src => uuids}/urandom.nim (100%) diff --git a/.gitignore b/.gitignore index 4532471..528610a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -**/uuids - .vscode/.browse.* nimsuggest.log diff --git a/src/uuids.nim b/uuids.nim similarity index 99% rename from src/uuids.nim rename to uuids.nim index 3127c31..b44ede2 100644 --- a/src/uuids.nim +++ b/uuids.nim @@ -1,6 +1,6 @@ import strutils, hashes import isaac -import urandom +import uuids/urandom type UUID* = object @@ -20,7 +20,7 @@ template toHex(s: string, start: Natural, if n == 0 and x < 0: n = -1 proc uuidsParseHexInt(s: string, maxLen: int): int64 = - if s.isNil or s.len == 0: + if s.len == 0: raise newException(ValueError, "UUID part is empty") if s.len > maxLen or s.len > sizeof(result) * 2: raise newException(ValueError, "UUID part is longer than expected") diff --git a/uuids.nimble b/uuids.nimble index 3fcc1fd..98eaf59 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -1,10 +1,9 @@ [Package] name: "uuids" -version: "0.1.9" +version: "0.1.10" author: "Xored Software, Inc." description: "UUID library" license: "MIT" -srcDir: "src" [Deps] requires: "isaac >= 0.1.3" diff --git a/src/urandom.nim b/uuids/urandom.nim similarity index 100% rename from src/urandom.nim rename to uuids/urandom.nim From 8cb8720b567c6bcb261bd1c0f7491bdb5209ad06 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Fri, 15 Jan 2021 07:30:01 +0100 Subject: [PATCH 8/8] Fix error with styleCheck Before this commit, running `nim c --styleCheck:error` on a file that imports the `uuids` package would produce an error on Windows. `WINBOOL` is defined here: https://github.com/nim-lang/Nim/blob/52cf7280019c/lib/windows/winlean.nim#L33 Fixes: #5 --- uuids.nimble | 2 +- uuids/urandom.nim | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/uuids.nimble b/uuids.nimble index 98eaf59..ccc6e86 100644 --- a/uuids.nimble +++ b/uuids.nimble @@ -1,6 +1,6 @@ [Package] name: "uuids" -version: "0.1.10" +version: "0.1.11" author: "Xored Software, Inc." description: "UUID library" license: "MIT" diff --git a/uuids/urandom.nim b/uuids/urandom.nim index 3666214..6078de4 100644 --- a/uuids/urandom.nim +++ b/uuids/urandom.nim @@ -16,16 +16,16 @@ when defined(windows): proc CryptAcquireContext( phProv: ptr HCRYPTPROV, pszContainer: WideCString, pszProvider: WideCString, dwProvType: DWORD, dwFlags: DWORD - ): WinBool {.importc: "CryptAcquireContextW".} + ): WINBOOL {.importc: "CryptAcquireContextW".} else: proc CryptAcquireContext( phProv: ptr HCRYPTPROV, pszContainer: cstring, pszProvider: cstring, dwProvType: DWORD, dwFlags: DWORD - ): WinBool {.importc: "CryptAcquireContextA".} + ): WINBOOL {.importc: "CryptAcquireContextA".} proc CryptGenRandom( hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: pointer - ): WinBool {.importc: "CryptGenRandom".} + ): WINBOOL {.importc: "CryptGenRandom".} {.pop.}