File: rand.go

package info (click to toggle)
golang-github-xtaci-kcp 4.0-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports-sloppy
  • size: 260 kB
  • sloc: sh: 55; makefile: 4
file content (24 lines) | stat: -rw-r--r-- 636 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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[:])
}