Skip to content

Commit

Permalink
simulator: add support for GetRNG
Browse files Browse the repository at this point in the history
This is needed to be able to simulate the Gopher Badge code:
https://github.com/conejoninja/gopherbadge
  • Loading branch information
aykevl authored and deadprogram committed Apr 28, 2024
1 parent 441dfc9 commit d419cc1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/machine/machine_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

package machine

import (
"crypto/rand"
)

// Dummy machine package that calls out to external functions.

const deviceName = "generic"
Expand Down Expand Up @@ -253,3 +257,13 @@ var (
sercomSPIM6 = SPI{6}
sercomSPIM7 = SPI{7}
)

// GetRNG returns 32 bits of random data from the WASI random source.
func GetRNG() (uint32, error) {
var buf [4]byte
_, err := rand.Read(buf[:])
if err != nil {
return 0, err
}
return uint32(buf[0])<<0 | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24, nil
}

0 comments on commit d419cc1

Please sign in to comment.