This repository was archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
64 lines (57 loc) · 1.62 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
package main
import (
"html/template"
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/fsgmhoward/formcrypt.go"
)
func main() {
r := gin.Default()
store := cookie.NewStore([]byte("some_random_char"))
sessionName := "mySession"
r.Use(sessions.Sessions(sessionName, store))
r.LoadHTMLGlob("templates/*")
err := formcrypt.InitializeEngine(r, sessionName)
if err != nil {
log.Fatal("Unable to initialize StatikFS")
}
r.GET("/", func(c *gin.Context) {
key := formcrypt.Key{BitSize: 2048}
err := key.Generate()
if err != nil {
c.String(http.StatusInternalServerError, "error in generating the key: "+err.Error())
return
}
err = key.Store(c, false)
if err != nil {
c.String(http.StatusInternalServerError, "error in storing key: "+err.Error())
return
}
js := key.GetJavascriptSegment("form_id", []string{"password"})
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"js": template.HTML(js),
})
})
r.POST("/", func(c *gin.Context) {
key, err := formcrypt.Load(c, false)
if err != nil {
c.String(http.StatusInternalServerError, "error in loading key: "+err.Error())
return
}
plaintext, err := key.Decrypt(c.PostForm("password"))
if err != nil {
c.String(http.StatusInternalServerError, "error in decrypting data: "+err.Error())
return
}
err = formcrypt.Void(c, false)
if err != nil {
c.String(http.StatusInternalServerError, "error in deleting key: "+err.Error())
return
}
c.String(http.StatusOK, "Data received:\nfname="+c.PostForm("fname")+"\npassword="+plaintext)
})
r.Run(":65500")
}