-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (75 loc) · 1.94 KB
/
Copy pathmain.go
File metadata and controls
94 lines (75 loc) · 1.94 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
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
90
91
92
93
94
package main
import (
"fmt"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/gopenpgp/v2/helper"
"os"
)
type Keys struct {
Public string
Private string
}
func main() {
keys := generateKeys()
fmt.Println(keys.Public)
fmt.Println(keys.Private)
writeKeyToDisk(keys.Public, "public.key")
writeKeyToDisk(keys.Private, "private.key")
msg := encodeMSG("hello world", keys.Public)
readPublicKeyFromFile()
decodeMSG(msg, readPrivateKeyFromFile(), readPublicKeyFromFile())
}
func generateKeys() *Keys {
name := "testName"
email := "devlist@loadbalancer.org"
eckey, err := crypto.GenerateKey(name, email, "x25519", 0)
if err != nil {
fmt.Println(err)
}
privateKey, err := eckey.Armor()
publicKey, err := eckey.GetArmoredPublicKey()
return &Keys{
publicKey,
privateKey,
}
}
func writeKeyToDisk(keyValue string, filename string) {
f, err := os.Create(filename)
defer f.Close()
if err != nil {
panic(err)
}
_, err = f.WriteString(keyValue)
if err != nil {
panic(err)
}
}
func readPublicKeyFromFile() *crypto.KeyRing {
f, err := os.Open("public.key")
if err != nil {
panic(err)
}
publicKeyObj, err := crypto.NewKeyFromArmoredReader(f)
publicKeyRing, err := crypto.NewKeyRing(publicKeyObj)
return publicKeyRing
}
func readPrivateKeyFromFile() *crypto.KeyRing {
f, err := os.Open("private.key")
if err != nil {
panic(err)
}
privateKeyOjb, _ := crypto.NewKeyFromArmoredReader(f)
privateKeyRing, _ := crypto.NewKeyRing(privateKeyOjb)
return privateKeyRing
}
func encodeMSG(message string, publicKey string) string {
armor, _ := helper.EncryptMessageArmored(publicKey, message)
fmt.Println(armor)
return armor
}
func decodeMSG(message string, privateKeyRing *crypto.KeyRing, publicKeyRing *crypto.KeyRing) {
msg, _ := crypto.NewPGPMessageFromArmored(message)
decoded, _ := privateKeyRing.Decrypt(msg, publicKeyRing, crypto.GetUnixTime())
privateKeyRing.ClearPrivateParams()
fmt.Println(decoded.GetString())
}