package rand
import (
)
type ChaCha8 struct {
state chacha8rand.State
readBuf [8]byte
readLen int
}
func ( [32]byte) *ChaCha8 {
:= new(ChaCha8)
.state.Init()
return
}
func ( *ChaCha8) ( [32]byte) {
.state.Init()
.readLen = 0
.readBuf = [8]byte{}
}
func ( *ChaCha8) () uint64 {
for {
, := .state.Next()
if {
return
}
.state.Refill()
}
}
func ( *ChaCha8) ( []byte) ( int, error) {
if .readLen > 0 {
= copy(, .readBuf[len(.readBuf)-.readLen:])
.readLen -=
= [:]
}
for len() >= 8 {
byteorder.LEPutUint64(, .Uint64())
= [8:]
+= 8
}
if len() > 0 {
byteorder.LEPutUint64(.readBuf[:], .Uint64())
+= copy(, .readBuf[:])
.readLen = 8 - len()
}
return
}
func ( *ChaCha8) ( []byte) error {
, := cutPrefix(, []byte("readbuf:"))
if {
var []byte
, , = readUint8LengthPrefixed()
if ! {
return errors.New("invalid ChaCha8 Read buffer encoding")
}
.readLen = copy(.readBuf[len(.readBuf)-len():], )
}
return chacha8rand.Unmarshal(&.state, )
}
func (, []byte) ( []byte, bool) {
if len() < len() || string([:len()]) != string() {
return , false
}
return [len():], true
}
func ( []byte) (, []byte, bool) {
if len() == 0 || len() < int(1+[0]) {
return nil, nil, false
}
return [1 : 1+[0]], [1+[0]:], true
}
func ( *ChaCha8) ( []byte) ([]byte, error) {
if .readLen > 0 {
= append(, "readbuf:"...)
= append(, uint8(.readLen))
= append(, .readBuf[len(.readBuf)-.readLen:]...)
}
return append(, chacha8rand.Marshal(&.state)...), nil
}
func ( *ChaCha8) () ([]byte, error) {
return .AppendBinary(make([]byte, 0, 64))
}