-
-
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
Implement Crystal::System::Random for win32 #5539
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
require "c/ntsecapi" | ||
|
||
module Crystal::System::Random | ||
def self.random_bytes(buf : Bytes) : Nil | ||
raise NotImplementedError.new("Crystal::System::Random.random_bytes") | ||
if LibC.RtlGenRandom(buf, buf.size) == 0 | ||
raise WinError.new("RtlGenRandom") | ||
end | ||
end | ||
|
||
def self.next_u : UInt8 | ||
raise NotImplementedError.new("Crystal::System::Random.next_u") | ||
buf = uninitialized UInt8[1] | ||
random_bytes(buf.to_slice) | ||
buf.unsafe_as(UInt8) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "./winnt" | ||
|
||
@[Link("advapi32")] | ||
lib LibC | ||
fun RtlGenRandom = SystemFunction036(random_buffer : Void*, random_buffer_length : ULong) : BOOLEAN | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
lib LibC | ||
alias WORD = UInt16 | ||
alias BOOL = Int32 | ||
alias BYTE = UChar | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib LibC | ||
alias BOOLEAN = BYTE | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why the alias?
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.
Nevermind, I guess it's a #define
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.
RtlGenRandom is what it's called in the windows documentation. The point of keeping the windows names is to be able to find the documentation easilly, so I think this is correct. It's much easier to read this way anyway.
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.
Have a look to libsodium sysrandom. The function is known as RtlGenRandom, but the symbol is
SystemFunction036
and kind of private API, but everybody uses it (libsodium, go, etc) because it avoids bundling the whole crypto API, which is apparently quite big.An alias, here, is expected, yes :-)