Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Mar 14, 2021
1 parent d1e0932 commit c467017
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/std/sysrand.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
## | :--- | ----: |
## | Windows | `BCryptGenRandom`_ |
## | Linux | `getrandom`_ |
## | MacOSX | `getentropy`_ |
## | MacOSX | `SecRandomCopyBytes`_ |
## | IOS | `SecRandomCopyBytes`_ |
## | OpenBSD | `getentropy openbsd`_ |
## | FreeBSD | `getrandom freebsd`_ |
Expand Down Expand Up @@ -58,7 +58,7 @@ when defined(posix):
import std/posix

const
batchImplOS = defined(freebsd) or defined(openbsd) or (defined(macosx) and not defined(ios))
batchImplOS = defined(freebsd) or defined(openbsd)
batchSize {.used.} = 256

when batchImplOS:
Expand Down Expand Up @@ -214,7 +214,7 @@ elif defined(freebsd):
proc getRandomImpl(p: pointer, size: int): int {.inline.} =
result = getrandom(p, csize_t(size), 0)

elif defined(ios):
elif defined(macosx):
{.passL: "-framework Security".}

const errSecSuccess = 0 ## No error.
Expand All @@ -237,18 +237,20 @@ elif defined(ios):

result = secRandomCopyBytes(nil, csize_t(size), addr dest[0])

elif defined(macosx):
const sysrandomHeader = """#include <Availability.h>
#include <sys/random.h>
"""
# when macosx version which is more than 10.12 become common, use the implementation below
# and don't forget to define (defined(macosx) and not defined(osx))

proc getentropy(p: pointer, size: csize_t): cint {.importc: "getentropy", header: sysrandomHeader.}
# getentropy() fills a buffer with random data, which can be used as input
# for process-context pseudorandom generators like arc4random(3).
# The maximum buffer size permitted is 256 bytes.
# const sysrandomHeader = """#include <Availability.h>
# #include <sys/random.h>
# """

proc getRandomImpl(p: pointer, size: int): int {.inline.} =
result = getentropy(p, csize_t(size)).int
# proc getentropy(p: pointer, size: csize_t): cint {.importc: "getentropy", header: sysrandomHeader.}
# # getentropy() fills a buffer with random data, which can be used as input
# # for process-context pseudorandom generators like arc4random(3).
# # The maximum buffer size permitted is 256 bytes.

# proc getRandomImpl(p: pointer, size: int): int {.inline.} =
# result = getentropy(p, csize_t(size)).int

else:
template urandomImpl(result: var int, dest: var openArray[byte]) =
Expand Down

0 comments on commit c467017

Please sign in to comment.