-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathuuid.nim
176 lines (150 loc) · 5.07 KB
/
uuid.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#
# Ethereum KeyFile
# (c) Copyright 2018
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
## This module implements interface to cross-platform UUID
## generator.
##
## - ``Windows`` - using rpcrt4.dll's `UuidCreate()`.
## - ``Linux`` and ``Android`` - using `/proc/sys/kernel/random/uuid`.
## - ``MacOS`` and ``iOS`` - using `uuid_generate_random()`.
## - ``FreeBSD``, ``OpenBSD``, ``NetBSD``,
## ``DragonflyBSD`` - using `uuid_create()`.
{.push raises: [Defect].}
import stew/[byteutils, endians2, results]
from nimcrypto import stripSpaces
export results
type
UUID* = object
## Represents UUID object
data*: array[16, byte]
UuidResult*[T] = Result[T, cstring]
proc uuidFromString*(s: string): UuidResult[UUID] =
## Convert string representation of UUID into UUID object.
if len(s) != 36:
return err("uuid: length must be 36 bytes")
for i in 0..<len(s):
if s[i] notin {'A'..'F', '0'..'9', 'a'..'f', '-'}:
return err("uuid: invalid characters")
try:
var d = hexToSeqByte(stripSpaces(s))
var
a = uint32.fromBytesBE(d.toOpenArray(0, 3))
b = uint16.fromBytesBE(d.toOpenArray(4, 5))
c = uint16.fromBytesBE(d.toOpenArray(6, 7))
var ret: UUID
copyMem(addr ret.data[0], addr a, 4)
copyMem(addr ret.data[4], addr b, 2)
copyMem(addr ret.data[6], addr c, 2)
copyMem(addr ret.data[8], addr d[8], 8)
ok(ret)
except CatchableError:
err("uuid: cannot parse hex string")
proc uuidToString*(u: UUID): string =
## Convert UUID object into string representation.
## UUID are lowercase, per RFC4122
result = newStringOfCap(38)
var d: array[8, byte]
var
a = uint32.fromBytesBE(u.data.toOpenArray(0, 3))
b = uint16.fromBytesBE(u.data.toOpenArray(4, 5))
c = uint16.fromBytesBE(u.data.toOpenArray(6, 7))
copyMem(addr d[0], addr a, 4)
copyMem(addr d[4], addr b, 2)
copyMem(addr d[6], addr c, 2)
result.add(toHex(toOpenArray(d, 0, 3)))
result.add("-")
result.add(toHex(toOpenArray(d, 4, 5)))
result.add("-")
result.add(toHex(toOpenArray(d, 6, 7)))
result.add("-")
result.add(toHex(toOpenArray(u.data, 8, 9)))
result.add("-")
result.add(toHex(toOpenArray(u.data, 10, 15)))
proc `$`*(u: UUID): string =
## Convert UUID object to lowercase string representation.
uuidToString(u)
when defined(nimdoc):
proc uuidGenerate*(): UuidResult[UUID]
## Generates new unique UUID and store it to `output`.
##
## Return 1 on success, and 0 on failure
when defined(posix):
when defined(macosx):
proc uuidGenerateRandom(a: pointer)
{.importc: "uuid_generate_random", header: "uuid/uuid.h".}
proc uuidGenerate*(): UuidResult[UUID] =
var output: UUID
uuidGenerateRandom(cast[pointer](addr output))
ok(output)
elif defined(freebsd) or defined(netbsd) or defined(openbsd) or
defined(dragonflybsd):
proc uuidCreate(a: pointer, s: ptr uint32)
{.importc: "uuid_create", header: "uuid.h".}
proc uuidGenerate*(): UuidResult[UUID] =
var status: uint32 = 0
var output: UUID
uuidCreate(cast[pointer](addr output), addr status)
if status == 0:
ok(output)
else:
err("uuid: uuid_create failed")
elif defined(linux) or defined(android):
import posix, os, nimcrypto/sysrand
proc uuidRead(bytes: var string, length: int): int =
result = -1
let fd = posix.open("/proc/sys/kernel/random/uuid", posix.O_RDONLY)
if fd != -1:
result = 0
while result < length:
var p = cast[pointer](cast[uint](addr bytes[0]) + uint(result))
var res = posix.read(fd, p, length - result)
if res > 0:
result += res
elif res == 0:
break
else:
if osLastError().int32 != EINTR:
result = -1
break
discard posix.close(fd)
proc uuidGenerate*(): UuidResult[UUID] =
var buffer = newString(37)
if uuidRead(buffer, 36) == 36:
buffer.setLen(36)
uuidFromString(buffer)
else:
var output: UUID
if randomBytes(output.data) == sizeof(output.data):
ok(output)
else:
err("uuid: cannot get random bytes")
else:
import nimcrypto/sysrand
proc uuidGenerate*(): UuidResult[UUID] =
var output: UUID
if randomBytes(output.data) == sizeof(output.data):
ok(output)
else:
err("uuid: cannot get random bytes")
elif defined(windows):
proc UuidCreate(p: pointer): int32
{.stdcall, dynlib: "rpcrt4", importc: "UuidCreate".}
proc uuidGenerate*(): UuidResult[UUID] =
var output: UUID
if UuidCreate(cast[pointer](addr output)) == 0:
ok(output)
else:
err("uuid: UuidCreate failed")
elif not defined(nimdoc):
import nimcrypto/sysrand
proc uuidGenerate*(): UuidResult[UUID] =
var output: UUID
if randomBytes(output.data) == sizeof(output.data):
ok(output)
else:
err("uuid: cannot get random bytes")