55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
"github.com/ProtonMail/gopenpgp/v2/helper"
|
|
)
|
|
|
|
const key = "3pw0c8#6ZG8{75b5;3?fe80$2"
|
|
|
|
type Identity struct {
|
|
ServerName string `json:"servername,omitempty"`
|
|
ServerDesc string `json:"servername,omitempty"`
|
|
PublicKey string `json:"public_key,omitempty"`
|
|
PrivateKey string `json:"private_key,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
// KnownServers ServerList `json:"known_servers,omitempty"`
|
|
}
|
|
|
|
func CreateIdentity(servername string, serverdesc string) *Identity {
|
|
var id Identity
|
|
id.ServerName = servername
|
|
id.ServerDesc = serverdesc
|
|
kp := meowlib.NewKeyPair()
|
|
id.PublicKey = kp.Public
|
|
id.PrivateKey = kp.Private
|
|
return &id
|
|
}
|
|
|
|
func LoadIdentity(file string) (*Identity, error) {
|
|
var id Identity
|
|
indata, err := ioutil.ReadFile(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pass, err := helper.DecryptMessageWithPassword([]byte(key), string(indata))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = json.Unmarshal([]byte(pass), &id)
|
|
return &id, err
|
|
}
|
|
|
|
func (id *Identity) Save(file string) error {
|
|
b, _ := json.Marshal(id)
|
|
armor, err := helper.EncryptMessageWithPassword([]byte(key), string(b))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = ioutil.WriteFile(file, []byte(armor), 0644)
|
|
return err
|
|
}
|