mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 05:23:40 +00:00
runtime: add runtime.rand function
This function is needed for Go 1.22, and is used from various packages like math/rand. When there is no random number generator available, it falls back to a static sequence of numbers. I think this is fine, because as far as I can see it is only used for non-cryptographic needs.
This commit is contained in:
@@ -228,3 +228,19 @@ func procPin() {
|
||||
//go:linkname procUnpin sync/atomic.runtime_procUnpin
|
||||
func procUnpin() {
|
||||
}
|
||||
|
||||
func hardwareRand() (n uint64, ok bool) {
|
||||
var n1, n2 uint32
|
||||
errCode1 := libc_rand_s(&n1)
|
||||
errCode2 := libc_rand_s(&n2)
|
||||
n = uint64(n1)<<32 | uint64(n2)
|
||||
ok = errCode1 == 0 && errCode2 == 0
|
||||
return
|
||||
}
|
||||
|
||||
// Cryptographically secure random number generator.
|
||||
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/rand-s?view=msvc-170
|
||||
// errno_t rand_s(unsigned int* randomValue);
|
||||
//
|
||||
//export rand_s
|
||||
func libc_rand_s(randomValue *uint32) int32
|
||||
|
||||
Reference in New Issue
Block a user