-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from 1 commit
0c817ca
c5b5c4a
e633bce
57d327f
cfbfc43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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), | ||
|
@@ -17,15 +19,19 @@ require "crystal/system/random" | |
# For generating *high quality* random numbers, a pseudorandom number generator | ||
# (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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😭 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
There was a problem hiding this comment.
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.