mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Merge pull request #1577 from zhibog/crypto_rand_windows
Added rand_bytes for Windows in core:crypto
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
when ODIN_OS != .Linux && ODIN_OS != .OpenBSD {
|
when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows {
|
||||||
_rand_bytes :: proc (dst: []byte) {
|
_rand_bytes :: proc (dst: []byte) {
|
||||||
unimplemented("crypto: rand_bytes not supported on this OS")
|
unimplemented("crypto: rand_bytes not supported on this OS")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import win32 "core:sys/windows"
|
||||||
|
import "core:os"
|
||||||
|
import "core:fmt"
|
||||||
|
|
||||||
|
_rand_bytes :: proc(dst: []byte) {
|
||||||
|
ret := (os.Errno)(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG))
|
||||||
|
if ret != os.ERROR_NONE {
|
||||||
|
switch ret {
|
||||||
|
case os.ERROR_INVALID_HANDLE:
|
||||||
|
// The handle to the first parameter is invalid.
|
||||||
|
// This should not happen here, since we explicitly pass nil to it
|
||||||
|
panic("crypto: BCryptGenRandom Invalid handle for hAlgorithm")
|
||||||
|
case os.ERROR_INVALID_PARAMETER:
|
||||||
|
// One of the parameters was invalid
|
||||||
|
panic("crypto: BCryptGenRandom Invalid parameter")
|
||||||
|
case:
|
||||||
|
// Unknown error
|
||||||
|
panic(fmt.tprintf("crypto: BCryptGenRandom failed: %d\n", ret))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user