76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package meshcore
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
|
|
"git.maze.io/go/ham/protocol/meshcore/crypto"
|
|
)
|
|
|
|
type Identity struct {
|
|
Name string
|
|
PrivateKey *crypto.PrivateKey
|
|
}
|
|
|
|
func (id *Identity) Hash() uint8 {
|
|
return id.PrivateKey.PublicKey()[0]
|
|
}
|
|
|
|
type Contact struct {
|
|
Name string
|
|
PublicKey *crypto.PublicKey
|
|
}
|
|
|
|
func (contact *Contact) Hash() uint8 {
|
|
return contact.PublicKey.Bytes()[0]
|
|
}
|
|
|
|
type Group struct {
|
|
Name string `json:"name"`
|
|
Hash [32]byte `json:"hash"`
|
|
Secret crypto.SharedSecret `json:"-"`
|
|
}
|
|
|
|
func (group Group) ChannelHash() uint8 {
|
|
return group.Hash[0]
|
|
}
|
|
|
|
func (group *Group) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(map[string]string{
|
|
"name": group.Name,
|
|
"hash": hex.EncodeToString(group.Hash[:]),
|
|
})
|
|
}
|
|
|
|
func (group *Group) UnmarshalJSON(b []byte) error {
|
|
var kv = make(map[string]string)
|
|
if err := json.Unmarshal(b, &kv); err != nil {
|
|
return err
|
|
}
|
|
group.Name = kv["name"]
|
|
h, err := hex.DecodeString(kv["hash"])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
copy(group.Hash[:], h)
|
|
return nil
|
|
}
|
|
|
|
func PublicGroup(name string) *Group {
|
|
h := sha256.Sum256([]byte(name))
|
|
return &Group{
|
|
Name: name,
|
|
Hash: sha256.Sum256(h[:16]),
|
|
Secret: crypto.MakeSharedSecretFromGroupSecret(h[:16]),
|
|
}
|
|
}
|
|
|
|
func SecretGroup(name string, key []byte) *Group {
|
|
return &Group{
|
|
Name: name,
|
|
Hash: sha256.Sum256(key),
|
|
Secret: crypto.MakeSharedSecret(key),
|
|
}
|
|
}
|