lux/net/lux_noncelist.go
2025-01-13 08:49:28 +02:00

50 lines
857 B
Go

package net
import "math/rand/v2"
const LUX_NONCE_MAX = 128
// Anti replay measure.
type LuxNonceList struct {
nonces []uint64
}
func GenerateLuxNonce() uint64 {
return rand.Uint64()
}
func NewLuxNonceList() LuxNonceList {
return LuxNonceList{
nonces: make([]uint64, 0),
}
}
func (list *LuxNonceList) RotateOrFail(newNonce uint64) bool {
for _, nonce := range list.nonces {
if nonce == newNonce {
return false
}
}
if len(list.nonces) == LUX_NONCE_MAX {
// rotate
for i := LUX_NONCE_MAX - 1; i > 0; i-- {
list.nonces[i] = list.nonces[i-1]
}
list.nonces[0] = newNonce
} else {
// append to head
list.nonces = append([]uint64{newNonce}, list.nonces...)
}
return true
}
func (list *LuxNonceList) Flush() {
list.nonces = make([]uint64, 0)
}
func (list *LuxNonceList) Get(idx int) uint64 {
return list.nonces[idx]
}