-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows to generate random numbers using a secure source provided by the system. It actually uses the same source as SecureRandom. Includes changes by Oleh Prypin (@prypin) to try and read as few bytes are required from `/dev/urandom`.
- Loading branch information
1 parent
0c6135b
commit 704a05b
Showing
8 changed files
with
106 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "spec" | ||
require "random/system" | ||
|
||
describe "Random::System" do | ||
rng = Random::System.new | ||
|
||
it "returns random number from the secure system source" do | ||
rng.next_u.should be_a(Int::Unsigned) | ||
|
||
x = rng.rand(123456...654321) | ||
x.should be >= 123456 | ||
x.should be < 654321 | ||
|
||
rng.rand(Int64::MAX / 2).should be <= (Int64::MAX / 2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "crystal/system/random" | ||
|
||
# Generates random numbers from a secure source of the system. | ||
# | ||
# For example `arc4random` is used on OpenBSD, whereas on Linux it uses | ||
# `getrandom` (if the kernel supports it) and fallbacks on reading from | ||
# `/dev/urandom` on UNIX systems. | ||
struct Random::System | ||
include Random | ||
|
||
def initialize | ||
end | ||
|
||
def next_u | ||
Crystal::System::Random.next_u | ||
end | ||
|
||
{% for type in [UInt8, UInt16, UInt32, UInt64] %} | ||
# Generates a random integer of a given type. The number of bytes to | ||
# generate can be limited; by default it will generate as many bytes as | ||
# needed to fill the integer size. | ||
private def rand_type(type : {{type}}.class, needed_parts = nil) : {{type}} | ||
needed_bytes = | ||
if needed_parts | ||
needed_parts * sizeof(typeof(next_u)) | ||
else | ||
sizeof({{type}}) | ||
end | ||
|
||
buf = uninitialized UInt8[sizeof({{type}})] | ||
|
||
if needed_bytes < sizeof({{type}}) | ||
bytes = Slice.new(buf.to_unsafe, needed_bytes) | ||
Crystal::System::Random.random_bytes(bytes) | ||
|
||
bytes.reduce({{type}}.new(0)) do |result, byte| | ||
(result << 8) | byte | ||
end | ||
else | ||
Crystal::System::Random.random_bytes(buf.to_slice) | ||
buf.to_unsafe.as({{type}}*).value | ||
end | ||
end | ||
{% end %} | ||
|
||
{% for type in [Int8, Int16, Int32, Int64] %} | ||
private def rand_type(type : {{type}}.class, needed_bytes = sizeof({{type}})) : {{type}} | ||
result = rand_type({{"U#{type}".id}}, needed_bytes) | ||
{{type}}.new(result) | ||
end | ||
{% end %} | ||
end |