Initial commit of pkg functions

This commit is contained in:
Laurence Withers 2023-03-01 21:28:48 +00:00
commit 6b6866077c
17 changed files with 1689 additions and 0 deletions

21
pkg/serial/rand.go Normal file
View file

@ -0,0 +1,21 @@
/*
Package serial provides serial number related functionality.
*/
package serial
import (
"crypto/rand"
"io"
"math/big"
)
const randBits = 160
// Rand returns a random serial number with 159 bits of entropy.
func Rand() *big.Int {
q := make([]byte, randBits/8)
io.ReadFull(rand.Reader, q)
// this just means all serial numbers are justified to the same bitlen, which is nice for tools!
q[0] |= 0x80
return big.NewInt(0).SetBytes(q)
}