mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-14 17:24:04 +00:00
72172d853c
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4614
26 lines
644 B
Go
26 lines
644 B
Go
package kcp
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"io"
|
|
)
|
|
|
|
// nonceMD5 is a nonce generator for each packet header
|
|
// which took the advantages of both MD5 and CSPRNG(like /dev/urandom).
|
|
// The benchmark shows it's faster than previous CSPRNG only method.
|
|
type nonceMD5 struct {
|
|
data [md5.Size]byte
|
|
}
|
|
|
|
// Nonce fills a nonce into the provided slice with no more than md5.Size bytes
|
|
// the entropy will be updated whenever a leading 0 appears
|
|
func (n *nonceMD5) Fill(nonce []byte) {
|
|
if n.data[0] == 0 { // 1/256 chance for entropy update
|
|
io.ReadFull(rand.Reader, n.data[:])
|
|
}
|
|
n.data = md5.Sum(n.data[:])
|
|
copy(nonce, n.data[:])
|
|
return
|
|
}
|