-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
172 lines (140 loc) · 3.83 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"log"
"math"
"os"
"strings"
"time"
"github.com/Shopify/ejson/crypto"
"github.com/atotto/clipboard"
)
const clipboardPreviewLength = 50
var stdin *bufio.Reader
var publicKeyBytes []byte
func init() {
stdin = bufio.NewReader(os.Stdin)
}
func main() {
if len(os.Args) != 2 {
usageAndDie()
}
switch os.Args[1] {
case "send":
sendSecret()
case "receive":
receiveSecret()
default:
usageAndDie()
}
}
func usageAndDie() {
fmt.Fprintln(os.Stderr, "usage: secret-sender send|receive")
os.Exit(1)
}
func sendSecret() {
fmt.Println("Ask the receiving party to run `secret-sender receive` and send you the public key that it generates.")
fmt.Println("Paste the public key here:")
publicKeyBytes = readline()
hexPublicKeyBytes, err := hex.DecodeString(string(publicKeyBytes))
if err != nil {
log.Fatal(err)
}
var receiverKP crypto.Keypair
for i, b := range hexPublicKeyBytes {
receiverKP.Public[i] = b
}
var ephemeralKP crypto.Keypair
if err := ephemeralKP.Generate(); err != nil {
log.Fatal(err)
}
fmt.Println("Copy your secret into your clipboard:")
plaintextResult := make(chan []byte)
go getClipboardContents(plaintextResult, "encrypt it")
plaintext := <-plaintextResult
encrypter := crypto.NewEncrypter(&ephemeralKP, receiverKP.Public)
ciphertext, err := encrypter.Encrypt(plaintext)
if err != nil {
log.Fatal(err)
}
copyToClipboard(string(ciphertext))
fmt.Println("This is the encrypted string. Paste it to the receiver. (We've already put it in your clipboard):")
fmt.Println(string(ciphertext))
}
func receiveSecret() {
var kp crypto.Keypair
if err := kp.Generate(); err != nil {
log.Fatal(err)
}
publicKey := kp.PublicString()
publicKeyBytes = []byte(publicKey)
copyToClipboard(publicKey)
fmt.Println("Paste this key to the sender (we've already put it in your clipboard):")
fmt.Println(publicKey)
fmt.Println("They'll respond with a big encrypted-looking blob. Once you receive it, copy it to your clipboard:")
ciphertextResult := make(chan []byte)
go getClipboardContents(ciphertextResult, "decrypt it")
ciphertext := <-ciphertextResult
decrypter := &crypto.Decrypter{Keypair: &kp}
plaintext, err := decrypter.Decrypt(ciphertext)
if err != nil {
log.Fatal(err)
}
fmt.Println("This is the secret:")
fmt.Println(string(plaintext))
}
func readline() []byte {
line, _, err := stdin.ReadLine()
if err != nil {
log.Fatal(err)
}
return line
}
func getClipboardContents(textResult chan []byte, action string) {
done := make(chan bool)
go getClipboardContentsInner(done, textResult, action)
go waitUntilEnter(done)
}
func getClipboardContentsInner(done chan bool, textResult chan []byte, action string) {
var clipboardContents []byte
for {
select {
case <-done:
textResult <- clipboardContents
return
default:
newClipboardContents := copyFromClipboard()
if bytes.Compare(newClipboardContents, clipboardContents) != 0 && bytes.Compare(newClipboardContents, publicKeyBytes) != 0 {
newClipboardPreview := strings.Join(strings.Fields(strings.TrimSpace(string(newClipboardContents))), " ")
newClipboardPreviewLength := math.Min(float64(len(newClipboardPreview)), clipboardPreviewLength)
fmt.Printf(
"Got new clipboard contents: length=%d preview='%s'. Press enter/return to %s.\n",
len(newClipboardContents),
newClipboardPreview[0:int(newClipboardPreviewLength)],
action,
)
clipboardContents = newClipboardContents
}
}
time.Sleep(1 * time.Second)
}
}
func waitUntilEnter(done chan bool) {
bufio.NewReader(os.Stdin).ReadBytes('\n')
done <- true
}
func copyToClipboard(text string) {
if err := clipboard.WriteAll(text); err != nil {
log.Fatal(err)
}
}
func copyFromClipboard() []byte {
text, err := clipboard.ReadAll()
if err != nil {
log.Fatal(err)
}
return []byte(text)
}