(no commit message provided)

This commit is contained in:
Prad Nukala
2024-07-05 22:20:13 -04:00
committed by Prad Nukala (aider)
commit 5fd43dfd6b
457 changed files with 115535 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
package random
import (
"crypto/rand"
"encoding/binary"
)
// GetRandomBytes randomly generates n bytes.
func GetRandomBytes(n uint32) []byte {
buf := make([]byte, n)
_, err := rand.Read(buf)
if err != nil {
panic(err) // out of randomness, should never happen
}
return buf
}
// GetRandomUint32 randomly generates an unsigned 32-bit integer.
func GetRandomUint32() uint32 {
b := GetRandomBytes(4)
return binary.BigEndian.Uint32(b)
}
+16
View File
@@ -0,0 +1,16 @@
package random_test
import (
"testing"
"github.com/onsonr/hway/crypto/subtle/random"
)
func TestGetRandomBytes(t *testing.T) {
for i := 0; i <= 32; i++ {
buf := random.GetRandomBytes(uint32(i))
if len(buf) != i {
t.Errorf("length of the output doesn't match the input")
}
}
}