rsa/pkg/serial/rand.go

22 lines
446 B
Go
Raw Normal View History

2023-03-01 21:28:48 +00:00
/*
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)
}