Skip to content

Commit 5c63b8a

Browse files
authored
Merge pull request #5 from Ybrin/master
Fix Linux randomUInt64 for Swift 4.1
2 parents 72adcd1 + 2184308 commit 5c63b8a

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

SipHash/RandomUInt64.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@
1212
func randomUInt64() -> UInt64 {
1313
return UInt64(arc4random()) << 32 | UInt64(arc4random())
1414
}
15-
#elseif os(Linux)
16-
import SwiftShims
15+
#elseif os(Linux) || os(FreeBSD)
16+
import Glibc
1717

1818
func randomUInt64() -> UInt64 {
19-
return UInt64(_swift_stdlib_cxx11_mt19937()) << 32 | UInt64(_swift_stdlib_cxx11_mt19937())
19+
var randomArray = [UInt8](repeating: 0, count: 8)
20+
21+
let fd = open("/dev/urandom", O_RDONLY)
22+
defer {
23+
close(fd)
24+
}
25+
26+
let _ = read(fd, &randomArray, MemoryLayout<UInt8>.size * 8)
27+
28+
var randomInt: UInt64 = 0
29+
for i in 0..<randomArray.count {
30+
randomInt = randomInt | (UInt64(randomArray[i]) << (i * 8))
31+
}
32+
33+
return randomInt
2034
}
2135
#else
2236
func randomUInt64() -> UInt64 {
2337
fatalError("Unsupported platform")
2438
}
2539
#endif
26-

0 commit comments

Comments
 (0)