Skip to content

Instantly share code, notes, and snippets.

@rdp
Created November 16, 2019 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdp/c9c20e1fcc3a52b860e73101ca917d61 to your computer and use it in GitHub Desktop.
Save rdp/c9c20e1fcc3a52b860e73101ca917d61 to your computer and use it in GitHub Desktop.
diff --git a/src/crystal/system/random.cr b/src/crystal/system/random.cr
index e03cc6ca6..745d0300d 100644
--- a/src/crystal/system/random.cr
+++ b/src/crystal/system/random.cr
@@ -11,7 +11,7 @@ module Crystal::System::Random
end
{% if flag?(:linux) %}
- require "./unix/getrandom"
+ require "./unix/urandom"
{% elsif flag?(:openbsd) %}
require "./unix/arc4random"
{% elsif flag?(:unix) %}
diff --git a/src/crystal/system/unix/urandom.cr b/src/crystal/system/unix/urandom.cr
index f2998b772..be9eeba30 100644
--- a/src/crystal/system/unix/urandom.cr
+++ b/src/crystal/system/unix/urandom.cr
@@ -1,4 +1,5 @@
-{% skip_file unless flag?(:unix) && !flag?(:openbsd) && !flag?(:linux) %}
+{% skip_file unless flag?(:unix) && !flag?(:openbsd) %}
+require "openssl"
module Crystal::System::Random
@@initialized = false
@@ -19,7 +20,11 @@ module Crystal::System::Random
init unless @@initialized
if urandom = @@urandom
- urandom.read_fully(buf)
+# urandom.read_fully(buf)
+ out = LibCrypto.rand_bytes(buf, buf.size)
+ if out != 1
+ raise "ossl failed"
+ end
else
raise "Failed to access secure source to generate random bytes!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment