# # 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()`. import nimcrypto/utils, stew/endians2 type UUIDException = object of CatchableError UUID* = object ## Represents UUID object data*: array[16, byte] proc raiseInvalidUuid() = raise newException(UUIDException, "Invalid UUID!") proc uuidFromString*(s: string): UUID = ## Convert string representation of UUID into UUID object. if len(s) != 36: raiseInvalidUuid() for i in 0.. 0: result += res elif res == 0: break else: if osLastError().int32 != EINTR: result = -1 break discard posix.close(fd) proc uuidGenerate*(output: var UUID): int = result = 0 var buffer = newString(37) if uuidRead(buffer, 36) == 36: buffer.setLen(36) output = uuidFromString(buffer) result = 1 else: if randomBytes(output.data) == sizeof(output.data): result = 1 else: import nimcrypto/sysrand proc uuidGenerate*(output: var UUID): int = if randomBytes(output.data) == sizeof(output.data): result = 1 else: result = 0 elif defined(windows): proc UuidCreate(p: pointer): int32 {.stdcall, dynlib: "rpcrt4", importc: "UuidCreate".} proc uuidGenerate*(output: var UUID): int = if UuidCreate(cast[pointer](addr output)) == 0: return 1 else: return 0 elif not defined(nimdoc): import nimcrypto/sysrand proc uuidGenerate*(output: var UUID): int = if randomBytes(output.data) == sizeof(output.data): result = 1 else: result = 0