mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-10 10:47:51 +08:00
align kubo with unified golang ci linter from IPDX and rules used in boxo and other go packages addressed lint rules: - ST1000: added package comments - ST1020, ST1021, ST1022: fixed function/method comments - QF1001: applied De Morgan's law - QF1003: converted if-else chains to tagged switches - QF1004: replaced strings.Replace with strings.ReplaceAll - QF1008: simplified embedded struct field selectors - unconvert: removed unnecessary type conversions - usestdlibvars: used stdlib constants instead of literals disabled errcheck linter in .golangci.yml
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package testutils
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"io"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
"golang.org/x/crypto/chacha20"
|
|
)
|
|
|
|
type randomReader struct {
|
|
cipher *chacha20.Cipher
|
|
remaining int64
|
|
}
|
|
|
|
func (r *randomReader) Read(p []byte) (int, error) {
|
|
if r.remaining <= 0 {
|
|
return 0, io.EOF
|
|
}
|
|
n := int64(len(p))
|
|
if n > r.remaining {
|
|
n = r.remaining
|
|
}
|
|
// Generate random bytes directly into the provided buffer
|
|
r.cipher.XORKeyStream(p[:n], make([]byte, n))
|
|
r.remaining -= n
|
|
return int(n), nil
|
|
}
|
|
|
|
// DeterministicRandomReader produces specified number of pseudo-random bytes
|
|
// from a seed.
|
|
func DeterministicRandomReader(sizeStr string, seed string) (io.Reader, error) {
|
|
size, err := humanize.ParseBytes(sizeStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Hash the seed string to a 32-byte key for ChaCha20
|
|
key := sha256.Sum256([]byte(seed))
|
|
// Use ChaCha20 for deterministic random bytes
|
|
var nonce [chacha20.NonceSize]byte // Zero nonce for simplicity
|
|
cipher, err := chacha20.NewUnauthenticatedCipher(key[:chacha20.KeySize], nonce[:])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &randomReader{cipher: cipher, remaining: int64(size)}, nil
|
|
}
|