ipld-eth-server/vendor/github.com/libp2p/go-libp2p-pnet/generate.go
Elizabeth Engelman 36533f7c3f Update vendor directory and make necessary code changes
Fixes for new geth version
2019-09-25 16:32:27 -05:00

39 lines
743 B
Go

package pnet
import (
"bytes"
"crypto/rand"
"encoding/hex"
"io"
)
func newLine() io.Reader {
return bytes.NewReader([]byte("\n"))
}
// GenerateV1PSK generates new PSK key that can be used with NewProtector
func GenerateV1PSK() (io.Reader, error) {
psk, err := GenerateV1Bytes()
if err != nil {
return nil, err
}
hexPsk := make([]byte, len(psk)*2)
hex.Encode(hexPsk, psk[:])
// just a shortcut to NewReader
nr := func(b []byte) io.Reader {
return bytes.NewReader(b)
}
return io.MultiReader(nr(pathPSKv1), newLine(), nr([]byte("/base16/")), newLine(), nr(hexPsk)), nil
}
func GenerateV1Bytes() (*[32]byte, error) {
psk := [32]byte{}
_, err := rand.Read(psk[:])
if err != nil {
return nil, err
}
return &psk, nil
}