1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:01:28 +00:00
This commit is contained in:
astaxie 2013-10-21 22:06:30 +08:00
parent 6ea8dd5999
commit 13180c5a17

View File

@ -217,24 +217,23 @@ func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Reque
//remote_addr cruunixnano randdata //remote_addr cruunixnano randdata
func (manager *Manager) sessionId(r *http.Request) (sid string) { func (manager *Manager) sessionId(r *http.Request) (sid string) {
b := make([]byte, 24) bs := make([]byte, 24)
if _, err := io.ReadFull(rand.Reader, b); err != nil { if _, err := io.ReadFull(rand.Reader, bs); err != nil {
return "" return ""
} }
bs := base64.URLEncoding.EncodeToString(b)
sig := fmt.Sprintf("%s%d%s", r.RemoteAddr, time.Now().UnixNano(), bs) sig := fmt.Sprintf("%s%d%s", r.RemoteAddr, time.Now().UnixNano(), bs)
if manager.hashfunc == "md5" { if manager.hashfunc == "md5" {
h := md5.New() h := md5.New()
h.Write([]byte(bs)) h.Write([]byte(sig))
sid = fmt.Sprintf("%s", hex.EncodeToString(h.Sum(nil))) sid = hex.EncodeToString(h.Sum(nil))
} else if manager.hashfunc == "sha1" { } else if manager.hashfunc == "sha1" {
h := hmac.New(sha1.New, []byte(manager.hashkey)) h := hmac.New(sha1.New, []byte(manager.hashkey))
fmt.Fprintf(h, "%s", sig) fmt.Fprintf(h, "%s", sig)
sid = fmt.Sprintf("%s", hex.EncodeToString(h.Sum(nil))) sid = hex.EncodeToString(h.Sum(nil))
} else { } else {
h := hmac.New(sha1.New, []byte(manager.hashkey)) h := hmac.New(sha1.New, []byte(manager.hashkey))
fmt.Fprintf(h, "%s", sig) fmt.Fprintf(h, "%s", sig)
sid = fmt.Sprintf("%s", hex.EncodeToString(h.Sum(nil))) sid = hex.EncodeToString(h.Sum(nil))
} }
return return
} }