-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
103 lines (92 loc) · 2.17 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/openpgp/armor"
"github.com/crowsonkb/base58"
"github.com/pkg/errors"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Need an action: one of mykey, send or receive")
os.Exit(1)
}
ensureIdentityKey()
switch os.Args[1] {
case "mykey":
printPublicKey()
case "send":
if len(os.Args) < 3 {
fmt.Println("Need email adress of recipient")
os.Exit(1)
}
send(os.Args[2])
case "receive":
if len(os.Args) < 3 {
fmt.Println("Need email adress of sender")
os.Exit(1)
}
receive(os.Args[2])
default:
fmt.Println("Unrecognized action:", os.Args[1])
fmt.Println("Need one of generate, send or receive")
os.Exit(1)
}
}
func ensureIdentityKey() {
_, err := os.Open("key")
if err != nil {
f, err := os.Create("key")
if err != nil {
log.Fatal(errors.Wrap(err, "Couldn't create private identity key"))
}
encoder, err := armor.Encode(f, "GOAX PRIVATE KEY", nil)
if err != nil {
log.Fatal(errors.Wrap(err, "Couldn't create armored writer"))
}
_, err = io.CopyN(encoder, rand.Reader, 32)
if err != nil {
log.Fatal(errors.Wrap(err, "Couldn't write private key to file"))
}
err = encoder.Close()
if err != nil {
log.Fatal(errors.Wrap(err, "Couldn't close encoder"))
}
err = f.Close()
if err != nil {
log.Fatal(errors.Wrap(err, "Couldn't close file"))
}
}
}
func printPublicKey() {
var myPublicKey [32]byte
var myPrivateKey [32]byte
copy(myPrivateKey[:], getPrivateKey())
curve25519.ScalarBaseMult(&myPublicKey, &myPrivateKey)
fmt.Println(base58.Encode(myPublicKey[:]))
}
func getPrivateKey() (pkey []byte) {
f, err := os.Open("key")
if err != nil {
log.Fatal(errors.Wrap(err, "Error opening private key"))
}
defer f.Close()
block, err := armor.Decode(f)
if err != nil {
log.Fatal(errors.Wrap(err, "Error decoding private key"))
}
private, err := ioutil.ReadAll(block.Body)
if err != nil {
log.Fatal(errors.Wrap(err, "Error decoding private key"))
}
return private
}
const (
ENCRYPTED_MESSAGE_TYPE string = "GOAX ENCRYPTED MESSAGE"
KEY_EXCHANGE_TYPE = "KEY EXCHANGE MATERIAL"
)