-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencryption.go
89 lines (68 loc) · 2.69 KB
/
encryption.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package bitcoin
import (
"encoding/hex"
"github.com/libsv/go-bk/bec"
)
// EncryptWithPrivateKey will encrypt the data using a given private key
func EncryptWithPrivateKey(privateKey *bec.PrivateKey, data string) (string, error) {
// Encrypt using bec
encryptedData, err := bec.Encrypt(privateKey.PubKey(), []byte(data))
if err != nil {
return "", err
}
// Return the hex encoded value
return hex.EncodeToString(encryptedData), nil
}
// DecryptWithPrivateKey is a wrapper to decrypt the previously encrypted
// information, given a corresponding private key
func DecryptWithPrivateKey(privateKey *bec.PrivateKey, data string) (string, error) {
// Decode the hex encoded string
rawData, err := hex.DecodeString(data)
if err != nil {
return "", err
}
// Decrypt the data
var decrypted []byte
if decrypted, err = bec.Decrypt(privateKey, rawData); err != nil {
return "", err
}
return string(decrypted), nil
}
// EncryptWithPrivateKeyString is a convenience wrapper for EncryptWithPrivateKey()
func EncryptWithPrivateKeyString(privateKey, data string) (string, error) {
// Get the private key from string
rawPrivateKey, err := PrivateKeyFromString(privateKey)
if err != nil {
return "", err
}
// Encrypt using bec
return EncryptWithPrivateKey(rawPrivateKey, data)
}
// DecryptWithPrivateKeyString is a convenience wrapper for DecryptWithPrivateKey()
func DecryptWithPrivateKeyString(privateKey, data string) (string, error) {
// Get private key
rawPrivateKey, _, err := PrivateAndPublicKeys(privateKey)
if err != nil {
return "", err
}
// Decrypt
return DecryptWithPrivateKey(rawPrivateKey, data)
}
// EncryptShared will encrypt data and provide shared keys for decryption
func EncryptShared(user1PrivateKey *bec.PrivateKey, user2PubKey *bec.PublicKey, data []byte) (
*bec.PrivateKey, *bec.PublicKey, []byte, error) {
// Generate shared keys that can be decrypted by either user
sharedPrivKey, sharedPubKey := GenerateSharedKeyPair(user1PrivateKey, user2PubKey)
// Encrypt data with shared key
encryptedData, err := bec.Encrypt(sharedPubKey, data)
return sharedPrivKey, sharedPubKey, encryptedData, err
}
// EncryptSharedString will encrypt a string to a hex encoded encrypted payload, and provide shared keys for decryption
func EncryptSharedString(user1PrivateKey *bec.PrivateKey, user2PubKey *bec.PublicKey, data string) (
*bec.PrivateKey, *bec.PublicKey, string, error) {
// Generate shared keys that can be decrypted by either user
sharedPrivKey, sharedPubKey := GenerateSharedKeyPair(user1PrivateKey, user2PubKey)
// Encrypt data with shared key
encryptedData, err := bec.Encrypt(sharedPubKey, []byte(data))
return sharedPrivKey, sharedPubKey, hex.EncodeToString(encryptedData), err
}