aboutsummaryrefslogtreecommitdiff
path: root/core/crypto/rand_windows.odin
blob: 53b58c776c5ff7313d952702775617f050fc7024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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))
		}
	}
}