1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00
wireguard-go/src/noise.go

56 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"encoding/hex"
"errors"
)
const (
NoisePublicKeySize = 32
NoisePrivateKeySize = 32
NoiseSymmetricKeySize = 32
)
type (
NoisePublicKey [NoisePublicKeySize]byte
NoisePrivateKey [NoisePrivateKeySize]byte
NoiseSymmetricKey [NoiseSymmetricKeySize]byte
NoiseNonce uint64 // padded to 12-bytes
)
2017-06-01 21:31:30 +02:00
func loadExactHex(dst []byte, src string) error {
slice, err := hex.DecodeString(src)
if err != nil {
return err
}
2017-06-01 21:31:30 +02:00
if len(slice) != len(dst) {
return errors.New("Hex string does not fit the slice")
}
2017-06-01 21:31:30 +02:00
copy(dst, slice)
return nil
}
2017-06-01 21:31:30 +02:00
func (key *NoisePrivateKey) FromHex(src string) error {
return loadExactHex(key[:], src)
}
func (key NoisePrivateKey) ToHex() string {
return hex.EncodeToString(key[:])
}
2017-06-01 21:31:30 +02:00
func (key *NoisePublicKey) FromHex(src string) error {
return loadExactHex(key[:], src)
}
func (key NoisePublicKey) ToHex() string {
return hex.EncodeToString(key[:])
}
func (key *NoiseSymmetricKey) FromHex(src string) error {
return loadExactHex(key[:], src)
}
2017-06-01 21:31:30 +02:00
func (key NoiseSymmetricKey) ToHex() string {
return hex.EncodeToString(key[:])
}