-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
63 lines (61 loc) · 1.16 KB
/
encrypt.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
58
59
60
61
62
63
package ecc
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
"crypto/sha256"
"errors"
)
func Encrypt(key crypto.PublicKey, data []byte) (encrypted []byte, err error) {
if len(data) < 1 {
err = errors.New("empty data")
return
}
public := key.(*PublicKey)
if public == nil {
err = errors.New("invalid public key")
return
}
private, err := GenerateKey()
if err != nil {
return
}
ephemeral := elliptic.Marshal(private.Curve, private.X, private.Y)
sym, _ := public.Curve.ScalarMult(public.X, public.Y, private.D)
// Create buffer
buf := bytes.Buffer{}
_, err = buf.Write(sym.Bytes())
if err != nil {
return
}
_, err = buf.Write([]byte{0x00, 0x00, 0x00, 0x01})
if err != nil {
return
}
_, err = buf.Write(ephemeral)
if err != nil {
return
}
hashed := sha256.Sum256(buf.Bytes())
buf.Reset()
block, err := aes.NewCipher(hashed[0:16])
if err != nil {
return
}
ch, err := cipher.NewGCMWithNonceSize(block, 16)
if err != nil {
return
}
_, err = buf.Write(ephemeral)
if err != nil {
return
}
_, err = buf.Write(ch.Seal(nil, hashed[16:], data, nil))
if err != nil {
return
}
encrypted = buf.Bytes()
return
}