34 lines
583 B
Go
34 lines
583 B
Go
package nonce
|
|
|
|
import (
|
|
"crypto/rand"
|
|
|
|
"github.com/indra-labs/indra/pkg/crypto/sha256"
|
|
)
|
|
|
|
const IDLen = 8
|
|
|
|
// ID is a value generated by a hash chain that renews at first use and every
|
|
// time it generates 65536 new ID's.
|
|
type ID [IDLen]byte
|
|
|
|
var seed sha256.Hash
|
|
var counter uint16
|
|
|
|
func reseed() {
|
|
if c, e := rand.Read(seed[:]); check(e) && c != IDLen {
|
|
}
|
|
counter++
|
|
}
|
|
|
|
// NewID returns a random 8 byte nonce to be used as identifiers.
|
|
func NewID() (t ID) {
|
|
if counter == 0 {
|
|
reseed()
|
|
}
|
|
s := sha256.Single(seed[:])
|
|
copy(seed[:], s[:])
|
|
copy(t[:], seed[:IDLen])
|
|
return
|
|
}
|