-
Notifications
You must be signed in to change notification settings - Fork 190
/
entity.go
37 lines (30 loc) · 955 Bytes
/
entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package db
import (
"github.com/brutella/hc/crypto"
"github.com/brutella/hc/util"
)
type Entity struct {
Name string
PublicKey []byte
PrivateKey []byte
}
// NewRandomEntityWithName returns an entity with a random private and public keys
func NewRandomEntityWithName(name string) (e Entity, err error) {
var public []byte
var private []byte
public, private, err = generateKeyPairs()
if err == nil && len(public) > 0 && len(private) > 0 {
e = NewEntity(name, public, private)
}
return
}
// NewEntity returns a entity with a name, public and private key.
func NewEntity(name string, publicKey, privateKey []byte) Entity {
return Entity{Name: name, PublicKey: publicKey, PrivateKey: privateKey}
}
// generateKeyPairs generates random public and private key pairs
func generateKeyPairs() ([]byte, []byte, error) {
str := util.RandomHexString()
public, private, err := crypto.ED25519GenerateKey(str)
return public, private, err
}