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

Implement Crystal::System::Random for win32 #5539

Merged
merged 1 commit into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions src/crystal/system/win32/random.cr
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
6 changes: 6 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/ntsecapi.cr
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
Copy link
Member

Choose a reason for hiding this comment

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

Why the alias?

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Contributor

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 :-)

end
1 change: 1 addition & 0 deletions src/lib_c/x86_64-windows-msvc/c/win_def.cr
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
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/winnt.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib LibC
alias BOOLEAN = BYTE
end