Skip to content

Commit

Permalink
Use arc4random_buf to fill random bytes on OpenBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed May 25, 2017
1 parent 0538f66 commit 0c6135b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/crystal/system/random.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ end

{% if flag?(:linux) %}
require "./unix/getrandom"
{% elsif flag?(:openbsd) %}
require "./unix/arc4random"
{% else %}
require "./unix/urandom"
{% end %}
10 changes: 10 additions & 0 deletions src/crystal/system/unix/arc4random.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "c/stdlib"

module Crystal::System::Random
# Fills *buffer* with random bytes using arc4random.
#
# NOTE: only secure on OpenBSD and CloudABI
def self.random_bytes(buffer : Bytes) : Nil
LibC.arc4random_buf(buffer.to_unsafe.as(Void*), buffer.size)
end
end
1 change: 1 addition & 0 deletions src/lib_c/amd64-unknown-openbsd/c/stdlib.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ lib LibC
rem : Int
end

fun arc4random_buf(x0 : Void*, x1 : SizeT) : Void
fun atof(x0 : Char*) : Double
fun div(x0 : Int, x1 : Int) : DivT
fun exit(x0 : Int) : NoReturn
Expand Down

0 comments on commit 0c6135b

Please sign in to comment.