-
Notifications
You must be signed in to change notification settings - Fork 0
/
dh.go
40 lines (31 loc) · 887 Bytes
/
dh.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
38
39
40
package security
import (
"crypto/sha256"
"fmt"
eciesgo "github.com/ecies/go/v2"
"github.com/stregato/master/woland/core"
)
func DiffieHellmanKey(identity Identity, id string) ([]byte, error) {
privateKey, _, err := DecodeKeys(identity.Private)
if core.IsErr(err, nil, "cannot decode keys: %v") {
return nil, err
}
publicKey, _, err := DecodeKeys(id)
if core.IsErr(err, nil, "cannot decode keys: %v") {
return nil, err
}
pr := eciesgo.NewPrivateKeyFromBytes(privateKey)
if pr == nil {
return nil, fmt.Errorf("cannot convert bytes to secp256k1 private key")
}
pu, err := eciesgo.NewPublicKeyFromBytes(publicKey)
if core.IsErr(err, nil, "cannot convert bytes to secp256k1 public key: %v") {
return nil, err
}
data, err := pr.ECDH(pu)
if core.IsErr(err, nil, "cannot perform ECDH: %v") {
return nil, err
}
h := sha256.Sum256(data)
return h[:], nil
}