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
|
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"snix.ir/ecookie"
)
type te struct {
IMMMMM int `json:"i"`
DMMMMM int `json:"d"`
Expire time.Time `json:"expire"`
SuperSecret string `json:"secret"`
}
var key = []byte{
0x11, 0x12, 0x13, 0x14,
0x11, 0x12, 0x13, 0x14,
0x11, 0x12, 0x13, 0x14,
0x11, 0x12, 0x13, 0x14,
}
func main() {
http.HandleFunc("/set", setHand)
http.HandleFunc("/get", getHand)
err := http.ListenAndServe(":8080", nil)
log.Fatal(err)
}
func setHand(w http.ResponseWriter, r *http.Request) {
x := new(te)
x.DMMMMM = 10000
x.IMMMMM = 90000
x.Expire = time.Now().Add(time.Hour)
x.SuperSecret = "some super secret data.. "
d, err := json.Marshal(x)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadGateway)
return
}
cc, err := ecookie.NewEncryptor(key)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadGateway)
return
}
ec, err := cc.Encrypt(d)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadGateway)
return
}
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: string(ec),
})
w.WriteHeader(http.StatusOK)
w.Write(ec)
}
func getHand(w http.ResponseWriter, r *http.Request) {
x, err := r.Cookie("auth")
if err != nil {
fmt.Println(err)
return
}
ee, err := ecookie.NewDecryptor(key)
if err != nil {
fmt.Println(err)
return
}
ll, err := ee.Decrypt([]byte(x.Value))
if err != nil {
fmt.Println(err)
return
}
vv := new(te)
err = json.Unmarshal(ll, vv)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(vv)
fmt.Fprintf(w, "%+v", *vv)
}
|