-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcrypto.go
More file actions
56 lines (45 loc) · 1.19 KB
/
crypto.go
File metadata and controls
56 lines (45 loc) · 1.19 KB
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
package main
import (
"github.com/ProtonMail/gopenpgp/v3/crypto"
)
// EncryptText encrypts plaintext using OpenPGP symmetric encryption with a password.
// Returns ASCII-armored encrypted text compatible with standard OpenPGP tools.
func EncryptText(plaintext, password string) (string, error) {
pgp := crypto.PGP()
encHandle, err := pgp.Encryption().
Password([]byte(password)).
Compress().
New()
if err != nil {
return "", err
}
pgpMessage, err := encHandle.Encrypt([]byte(plaintext))
if err != nil {
return "", err
}
armored, err := pgpMessage.ArmorBytes()
if err != nil {
return "", err
}
return string(armored), nil
}
// DecryptText decrypts ASCII-armored OpenPGP encrypted text using a password.
// Returns the plaintext content.
func DecryptText(encryptedText, password string) (string, error) {
pgp := crypto.PGP()
decHandle, err := pgp.Decryption().
Password([]byte(password)).
New()
if err != nil {
return "", err
}
pgpMessage, err := crypto.NewPGPMessageFromArmored(encryptedText)
if err != nil {
return "", err
}
decrypted, err := decHandle.Decrypt(pgpMessage.Bytes(), crypto.Auto)
if err != nil {
return "", err
}
return string(decrypted.Bytes()), nil
}