-
Notifications
You must be signed in to change notification settings - Fork 30
/
kazi.go
176 lines (138 loc) · 3.72 KB
/
kazi.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
173
174
175
176
package main
import (
"crypto/rand"
"html/template"
"log"
"net/http"
"os"
"time"
uuid "github.com/nu7hatch/gouuid"
"strings"
"fmt"
"io"
"encoding/hex"
"golang.org/x/crypto/nacl/secretbox"
"google.golang.org/appengine"
"google.golang.org/appengine/memcache"
)
type msgAndSecretKeys struct {
MsgKey string
SecretKey string
URLMsg string
}
var tpl *template.Template
func main() {
tpl = template.Must(template.ParseGlob("./*.html"))
http.HandleFunc("/", index)
http.HandleFunc("/msg/", message)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
// create a message
func index(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
var keySystem msgAndSecretKeys
if r.Method == http.MethodPost {
msg := r.FormValue("umsg")
mkey, _ := uuid.NewV4()
skey := generatePassword()
encryptedMessage := encrypt(msg, skey)
keySystem.MsgKey = mkey.String()
keySystem.SecretKey = fmt.Sprintf("%x", skey)
keySystem.URLMsg = "/msg/" + keySystem.MsgKey
// store message in memcache
item := &memcache.Item{
Key: keySystem.MsgKey,
Value: []byte(encryptedMessage),
}
err := memcache.Add(ctx, item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tpl.ExecuteTemplate(w, "secret.html", keySystem)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
err := tpl.ExecuteTemplate(w, "index.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
// return a message based on its id
func message(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
// extracting key from url
key := strings.SplitN(r.URL.Path, "/", 3)[2]
// extracting item from google appengine memcache
item, err := memcache.Get(ctx, key)
if err != nil {
http.NotFound(w, r)
return
}
var password [32]byte
bs, err := hex.DecodeString(r.FormValue("secret"))
if err != nil || len(bs) != 32 {
http.Error(w, err.Error(), 500)
return
}
copy(password[:], bs)
decryptedMessage, err := decrypt(string(item.Value), password)
if err != nil {
http.NotFound(w, r)
return
}
// memcache.Delete(ctx, key), use this for super duper tight security, message is burned the second its opened
// this one below for specified seconds expiration, will destroy message after some time
if item.Flags == 0 {
item.Expiration = 30 * time.Second
item.Flags = 1
memcache.Set(ctx, item)
}
err = tpl.ExecuteTemplate(w, "message.html", decryptedMessage)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func decrypt(encrypted string, password [32]byte) (string, error) {
var nonce [24]byte
parts := strings.SplitN(encrypted, ":", 2)
if len(parts) < 2 {
return "", fmt.Errorf("Expected nonce")
}
bs, err := hex.DecodeString(parts[0])
if err != nil || len(bs) != 24 {
return "", fmt.Errorf("invalid nonce")
}
copy(nonce[:], bs)
bs, err = hex.DecodeString(parts[1])
if err != nil {
return "", fmt.Errorf("invalid message")
}
decrypted, ok := secretbox.Open(nil, bs, &nonce, &password)
if !ok {
return "", fmt.Errorf("invalid message")
}
return string(decrypted), nil
}
func encrypt(decrypted string, password [32]byte) string {
var nonce [24]byte
io.ReadAtLeast(rand.Reader, nonce[:], 24)
encrypted := secretbox.Seal(nil, []byte(decrypted), &nonce, &password)
return fmt.Sprintf("%x:%x", nonce[:], encrypted)
}
func generatePassword() [32]byte {
var password [32]byte
io.ReadAtLeast(rand.Reader, password[:], 32)
return password
}