-
Notifications
You must be signed in to change notification settings - Fork 26
/
crypto.go
57 lines (48 loc) · 1.23 KB
/
crypto.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package bot
import (
"crypto/ed25519"
"crypto/sha512"
"sort"
"filippo.io/edwards25519"
"github.com/MixinNetwork/mixin/crypto"
"golang.org/x/crypto/curve25519"
)
func PrivateKeyToCurve25519(curve25519Private *[32]byte, privateKey ed25519.PrivateKey) {
h := sha512.New()
h.Write(privateKey.Seed())
digest := h.Sum(nil)
digest[0] &= 248
digest[31] &= 127
digest[31] |= 64
copy(curve25519Private[:], digest)
}
func PublicKeyToCurve25519(publicKey ed25519.PublicKey) ([]byte, error) {
p, err := (&edwards25519.Point{}).SetBytes(publicKey[:])
if err != nil {
return nil, err
}
return p.BytesMontgomery(), nil
}
func SharedKey(public ed25519.PublicKey, private ed25519.PrivateKey) ([32]byte, error) {
var dst, priv, pub [32]byte
curve25519Public, err := PublicKeyToCurve25519(public)
if err != nil {
return dst, err
}
PrivateKeyToCurve25519(&priv, private.Seed())
copy(pub[:], curve25519Public[:])
d, err := curve25519.X25519(priv[:], pub[:])
if err != nil {
return dst, err
}
copy(dst[:], d)
return dst, nil
}
func HashMembers(ids []string) string {
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
var in string
for _, id := range ids {
in = in + id
}
return crypto.NewHash([]byte(in)).String()
}