package server import ( "encoding/json" "fmt" "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:"serverdesc,omitempty"` ServerKp meowlib.KeyPair `json:"server_kp,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 id.ServerKp = meowlib.NewKeyPair() 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 } // AsymEncryptMessage prepares a message to send to a specific client contact func (id *Identity) AsymEncryptMessage(ClientPublicKey string, Message []byte) (EncryptedMsg []byte, Signature []byte, err error) { EncryptedMsg, Signature, err = meowlib.EncryptAndSign(ClientPublicKey, id.ServerKp.Private, Message) if err != nil { fmt.Println(err.Error()) return nil, nil, err } return EncryptedMsg, Signature, err } // AsymDecryptMessage reads a message from a specific client contact func (id *Identity) AsymDecryptMessage(ClientPublicKey string, Message []byte, Signature []byte) (DecryptedMsg []byte, err error) { DecryptedMsg, err = meowlib.DecryptAndCheck(id.ServerKp.Private, ClientPublicKey, Message, Signature) if err != nil { fmt.Println(err.Error()) return nil, err } return DecryptedMsg, err }