36 lines
725 B
Go
36 lines
725 B
Go
package engine
|
|
|
|
import (
|
|
"crypto/ecdh"
|
|
"encoding/hex"
|
|
)
|
|
|
|
type KeyPair struct {
|
|
PrivateKey *ecdh.PrivateKey
|
|
PublicKey *ecdh.PublicKey
|
|
}
|
|
|
|
func (kp *KeyPair) EncodePublicKey() string {
|
|
return hex.EncodeToString(kp.PublicKey.Bytes())
|
|
}
|
|
|
|
func (kp *KeyPair) EncodePrivateKey() string {
|
|
return hex.EncodeToString(kp.PrivateKey.Bytes())
|
|
}
|
|
|
|
func (e *Engine) DecodePrivateKey(hexKey string) (*ecdh.PrivateKey, error) {
|
|
bytes, err := hex.DecodeString(hexKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return e.curve.NewPrivateKey(bytes)
|
|
}
|
|
|
|
func (e *Engine) DecodePublicKey(hexKey string) (*ecdh.PublicKey, error) {
|
|
bytes, err := hex.DecodeString(hexKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return e.curve.NewPublicKey(bytes)
|
|
}
|