mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 04:23:41 +00:00
wasm: add support for the crypto/rand package
This is done via wasi-libc and the WASI interface, for ease of maintenance (only one implementation for both WASI and JS/browsers).
This commit is contained in:
committed by
Ron Evans
parent
ab47cea055
commit
478c592b13
Vendored
-23
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -21,26 +20,4 @@ func main() {
|
||||
for _, arg := range os.Args[1:] {
|
||||
println("arg:", arg)
|
||||
}
|
||||
|
||||
// Check for crypto/rand support.
|
||||
checkRand()
|
||||
}
|
||||
|
||||
func checkRand() {
|
||||
buf := make([]byte, 500)
|
||||
n, err := rand.Read(buf)
|
||||
if n != len(buf) || err != nil {
|
||||
println("could not read random numbers:", err)
|
||||
}
|
||||
|
||||
// Very simple test that random numbers are at least somewhat random.
|
||||
sum := 0
|
||||
for _, b := range buf {
|
||||
sum += int(b)
|
||||
}
|
||||
if sum < 95*len(buf) || sum > 159*len(buf) {
|
||||
println("random numbers don't seem that random, the average byte is", sum/len(buf))
|
||||
} else {
|
||||
println("random number check was successful")
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
-1
@@ -3,4 +3,3 @@ ENV2: VALUE2
|
||||
|
||||
arg: first
|
||||
arg: second
|
||||
random number check was successful
|
||||
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "crypto/rand"
|
||||
|
||||
// TODO: make this a test in the crypto/rand package.
|
||||
|
||||
func main() {
|
||||
buf := make([]byte, 500)
|
||||
n, err := rand.Read(buf)
|
||||
if n != len(buf) || err != nil {
|
||||
println("could not read random numbers:", err)
|
||||
}
|
||||
|
||||
// Very simple test that random numbers are at least somewhat random.
|
||||
sum := 0
|
||||
for _, b := range buf {
|
||||
sum += int(b)
|
||||
}
|
||||
if sum < 95*len(buf) || sum > 159*len(buf) {
|
||||
println("random numbers don't seem that random, the average byte is", sum/len(buf))
|
||||
} else {
|
||||
println("random number check was successful")
|
||||
}
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
random number check was successful
|
||||
Reference in New Issue
Block a user