/
encrypt.go
31 lines (26 loc) · 1.02 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
package crypto
import (
"crypto/rand"
"github.com/ethereum/go-ethereum/crypto/ecies"
)
// Encrypt uses ECIES algorithm to encrypt a message with a decompressed public key
func Encrypt(msg, decompressedPublicKey []byte) ([]byte, error) {
// As of the latest version, we assume the byte arrays are ECDSA-compatible keys
pk, err := ECDSAPublicKeyFrom(decompressedPublicKey)
if err != nil {
return nil, err
}
pubkey := (*ecies.ImportECDSAPublic(&pk))
return ecies.Encrypt(rand.Reader, &pubkey, msg, nil, nil)
}
// Decrypt recovers data encrypted with ECIES algorithm using the private key and the decompressed public key
// TODO Try not to use the public key, only the private
func Decrypt(ciphered, privateKey, decompressedPublicKey []byte) ([]byte, error) {
// As of the latest version, we assume the byte arrays are ECDSA-compatible keys
sk, err := ECDSAPrivateKeyFrom(privateKey, decompressedPublicKey)
if err != nil {
return nil, err
}
privkey := (*ecies.ImportECDSA(&sk))
return privkey.Decrypt(ciphered, nil, nil)
}