Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation for Random::Secure #8484

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! Improve documentation for Random::Secure
  • Loading branch information
straight-shoota committed Nov 18, 2019
commit c5b5c4a9ad3155aac17afa9e6b0f88dd13e172bb
18 changes: 12 additions & 6 deletions src/random/secure.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require "crystal/system/random"

# `Random::Secure` provides a [cryptographically secure pseudorandom number generator (CSRPNG)](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator)
# for cryptography and secure usages such as generating secret keys, or seeding a pseudorandom number generator (PRNG).
# `Random::Secure` generates random numbers from a secure source provided by the system.
#
# It uses a [cryptographically secure pseudorandom number generator (CSRPNG)](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator)
# for cryptography and secure usages such as generating secret keys, or seeding
# a pseudorandom number generator (PRNG).
#
# ```
# Random::Secure.rand(6) # => 4
# [1, 5, 6].shuffle(Random::Secure) # => [6, 1, 5]
# ```
#
# It uses a secure source provided by the operating system.
# On OpenBSD, it uses [`arc4random`](https://man.openbsd.org/arc4random),
# on Linux [`getrandom`](http://man7.org/linux/man-pages/man2/getrandom.2.html) (if the kernel supports it),
# on Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom),
Expand All @@ -17,15 +19,19 @@ require "crystal/system/random"
# For generating *high quality* random numbers, a pseudorandom number generator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather remove this whole paragraph. "high quality" is pretty confusing and undefined.

# (PRNG) such as `Random::PCG32` should be used instead, or preferably one with
# more state (e.g. `xoshiro256**`). These number generators are much faster
# than `Random::Secure`.
# than `Random::Secure` and suitable for many use cases with lower
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without instead of lower.

For a slightly lower security (because of user-space instead of kernel), an user-space CSPRNG such as ISAAC or ChaCha20 can be used.

Yes, RNG and security is complex and full of devils in tiny details. I barely grasp them 😭

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish someone with good knowledge of RNG and such details would write such documentation.

# cryptographic security demands. The values generated by pseudorandom number
# generators are easier to predict. When this could be exploited, it's better
# to use `Random::Secure`.
module Random::Secure
extend Random
extend self

def self.next_u
def next_u
Crystal::System::Random.next_u
end

def self.random_bytes(buf : Bytes)
def random_bytes(buf : Bytes)
Crystal::System::Random.random_bytes(buf)
end

Expand Down