package main import ( "crypto/md5" "crypto/rand" "encoding/hex" "net/http" ) func randomToken(length int) string { b := make([]byte, length) rand.Read(b) return hex.EncodeToString(b) } func genKey(length int) []byte { b := make([]byte, length) rand.Read(b) return b } func md5hex(s string) string { hash := md5.Sum([]byte(s)) return hex.EncodeToString(hash[:]) } func (app *App) GetCookie(r *http.Request, name string, dst interface{}) error { cookie, err := r.Cookie(name) if err != nil { return err } return app.CookieHandler.Decode(name, cookie.Value, dst) } func (app *App) SetCookie(w http.ResponseWriter, name string, v interface{}, exp int) { encoded, _ := app.CookieHandler.Encode(name, v) cookie := &http.Cookie{ Name: name, Value: encoded, Path: "/", HttpOnly: true, MaxAge: exp, } http.SetCookie(w, cookie) }