Files
ionscale/internal/util/key.go
T
Johan Siebens 5ad89ff02f initial working version
Signed-off-by: Johan Siebens <johan.siebens@gmail.com>
2022-05-09 21:54:06 +02:00

70 lines
1.4 KiB
Go

package util
import (
"strings"
"tailscale.com/types/key"
)
const (
discoPublicHexPrefix = "discokey:"
nodePublicHexPrefix = "nodekey:"
machinePublicHexPrefix = "mkey:"
privateHexPrefix = "privkey:"
)
func ParseMachinePrivateKey(machineKey string) (*key.MachinePrivate, error) {
if !strings.HasPrefix(machineKey, privateHexPrefix) {
machineKey = privateHexPrefix + machineKey
}
var mp key.MachinePrivate
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
return nil, err
}
return &mp, nil
}
func ParseMachinePublicKey(machineKey string) (*key.MachinePublic, error) {
if !strings.HasPrefix(machineKey, machinePublicHexPrefix) {
machineKey = machinePublicHexPrefix + machineKey
}
var mp key.MachinePublic
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
return nil, err
}
return &mp, nil
}
func ParseNodePublicKey(machineKey string) (*key.NodePublic, error) {
if !strings.HasPrefix(machineKey, nodePublicHexPrefix) {
machineKey = nodePublicHexPrefix + machineKey
}
var mp key.NodePublic
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
return nil, err
}
return &mp, nil
}
func ParseDiscoPublicKey(machineKey string) (*key.DiscoPublic, error) {
if !strings.HasPrefix(machineKey, discoPublicHexPrefix) {
machineKey = discoPublicHexPrefix + machineKey
}
var mp key.DiscoPublic
if err := mp.UnmarshalText([]byte(machineKey)); err != nil {
return nil, err
}
return &mp, nil
}