123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package session
- import (
- "bytes"
- "crypto/rand"
- "encoding/gob"
- "io"
- "github.com/Unknwon/com"
- )
- func init() {
- gob.Register([]interface{}{})
- gob.Register(map[int]interface{}{})
- gob.Register(map[string]interface{}{})
- gob.Register(map[interface{}]interface{}{})
- gob.Register(map[string]string{})
- gob.Register(map[int]string{})
- gob.Register(map[int]int{})
- gob.Register(map[int]int64{})
- }
- func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) {
- for _, v := range obj {
- gob.Register(v)
- }
- buf := bytes.NewBuffer(nil)
- err := gob.NewEncoder(buf).Encode(obj)
- return buf.Bytes(), err
- }
- func DecodeGob(encoded []byte) (out map[interface{}]interface{}, err error) {
- buf := bytes.NewBuffer(encoded)
- err = gob.NewDecoder(buf).Decode(&out)
- return out, err
- }
- var alphanum = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
- func generateRandomKey(strength int) []byte {
- k := make([]byte, strength)
- if n, err := io.ReadFull(rand.Reader, k); n != strength || err != nil {
- return com.RandomCreateBytes(strength, alphanum...)
- }
- return k
- }
|