Created
January 17, 2023 06:42
-
-
Save deadprogram/b39ffffc6ddc035350a1d3b26cf9c71f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOT true random number generation on RP2040 but good enough for quick demos. | |
// Do not use in important production systems. | |
// Seriously, don't. | |
package main | |
import ( | |
"machine" | |
"crypto/rand" | |
) | |
func init() { | |
rand.Reader = &reader{} | |
} | |
type reader struct {} | |
func (r *reader) Read(b []byte) (n int, err error) { | |
if len(b) == 0 { | |
return | |
} | |
var randomByte uint32 | |
for i := range b { | |
if i%4 == 0 { | |
randomByte, err = machine.GetRNG() | |
if err != nil { | |
return n, err | |
} | |
} else { | |
randomByte >>= 8 | |
} | |
b[i] = byte(randomByte) | |
} | |
return len(b), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment