Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Sep 3, 2015
1 parent 87a8397 commit 22da862
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 40 deletions.
9 changes: 5 additions & 4 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/coscms/xweb/httpsession"
"github.com/coscms/xweb/lib/str"
"github.com/coscms/xweb/log"
"github.com/coscms/xweb/uuid"
)
Expand Down Expand Up @@ -446,7 +447,7 @@ func (c *Action) SetSecureCookie(name string, val string, args ...interface{}) {
c.App.Error("Secret Key for secure cookies has not been set. Please assign a cookie secret to web.Config.CookieSecret.")
return
}
vs := Base64Encode(val)
vs := str.Base64Encode(val)
vb := []byte(vs)
key := c.App.AppConfig.CookieSecret
if c.App.AppConfig.CookieLimitIP {
Expand All @@ -456,7 +457,7 @@ func (c *Action) SetSecureCookie(name string, val string, args ...interface{}) {
key += "|" + c.UserAgent()
}
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
sig := getCookieSig(key, vb, timestamp)
sig := str.Token(key, vb, timestamp)
cookie := strings.Join([]string{vs, timestamp, sig}, "|")
c.SetCookie(c.NewCookie(name, cookie, args...))
}
Expand All @@ -481,7 +482,7 @@ func (c *Action) GetSecureCookie(name string) (string, bool) {
timestamp := parts[1]
sig := parts[2]

if getCookieSig(key, []byte(val), timestamp) != sig {
if str.Token(key, []byte(val), timestamp) != sig {
c.SetCookie(NewCookie(name, "", -86400))
return "", false
}
Expand All @@ -493,7 +494,7 @@ func (c *Action) GetSecureCookie(name string) (string, bool) {
return "", false
}

return Base64Decode(val), true
return str.Base64Decode(val), true
}
return "", false
}
Expand Down
5 changes: 3 additions & 2 deletions cryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"github.com/coscms/xweb/lib/str"
"log"
)

Expand Down Expand Up @@ -60,11 +61,11 @@ func (c *AesCrypto) Encode(rawData, authKey string) string {
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(in))
blockMode.CryptBlocks(crypted, in)
return Base64Encode(string(crypted))
return str.Base64Encode(string(crypted))
}

func (c *AesCrypto) Decode(cryptedData, authKey string) string {
cryptedData = Base64Decode(cryptedData)
cryptedData = str.Base64Decode(cryptedData)
if cryptedData == "" {
return ""
}
Expand Down
29 changes: 0 additions & 29 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package xweb

import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -267,16 +263,6 @@ func XsrfName() string {
return XSRF_TAG
}

func getCookieSig(key string, val []byte, timestamp string) string {
hm := hmac.New(sha1.New, []byte(key))

hm.Write(val)
hm.Write([]byte(timestamp))

hex := fmt.Sprintf("%02x", hm.Sum(nil))
return hex
}

func redirect(w http.ResponseWriter, url string, status ...int) error {
s := 302
if len(status) > 0 {
Expand All @@ -300,18 +286,3 @@ func Download(w http.ResponseWriter, fpath string) error {
_, err = io.Copy(w, f)
return err
}

func Base64Encode(val string) string {
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
encoder.Write([]byte(val))
encoder.Close()
return strings.TrimRight(buf.String(), "=")
}

func Base64Decode(val string) string {
buf := bytes.NewBufferString(val)
encoder := base64.NewDecoder(base64.StdEncoding, buf)
res, _ := ioutil.ReadAll(encoder)
return string(res)
}
10 changes: 5 additions & 5 deletions httpsession/memcachestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

CS "coscms/app/base/lib/cachestore"
"github.com/coscms/xweb/lib/str"
"github.com/bradfitz/gomemcache/memcache"
)

Expand Down Expand Up @@ -66,7 +66,7 @@ func (store *MemcacheStore) get(id Id) *sessionNode {
}

var v interface{}
err = CS.Decode(val.Value, &v)
err = str.Decode(val.Value, &v)
if err != nil {
if store.Debug {
log.Println("[Memcache]DecodeErr: ", err, "Key:", key)
Expand All @@ -77,7 +77,7 @@ func (store *MemcacheStore) get(id Id) *sessionNode {
}
func (store *MemcacheStore) set(id Id, v *sessionNode) bool {
key := string(id)
val, err := CS.Encode(v)
val, err := str.Encode(v)
if err != nil {
if store.Debug {
log.Println("[Memcache]EncodeErr: ", err, "Key:", key)
Expand Down Expand Up @@ -109,7 +109,7 @@ func (store *MemcacheStore) Del(id Id, key string) bool {

func (store *MemcacheStore) Exist(id Id) bool {
key := string(id)
mk := CS.Md5(key)
mk := str.Md5(key)
val, err := store.c.Get(mk)
if err != nil || val == nil {
return false
Expand All @@ -119,7 +119,7 @@ func (store *MemcacheStore) Exist(id Id) bool {

func (store *MemcacheStore) Clear(id Id) bool {
key := string(id)
err := store.c.Delete(CS.Md5(key))
err := store.c.Delete(str.Md5(key))
if err != nil {
if store.Debug {
log.Println("[Memcache]DelErr: ", err, "Key:", key)
Expand Down
86 changes: 86 additions & 0 deletions lib/str/encdec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package str

import (
"bytes"
"crypto/md5"
"encoding/gob"
"encoding/json"
"crypto/hmac"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"encoding/base64"
"strings"
)


// md5 hash string
func Md5(str string) string {
m := md5.New()
io.WriteString(m, str)
return fmt.Sprintf("%x", m.Sum(nil))
}


func Token(key string, val []byte, args ...string) string {
hm := hmac.New(sha1.New, []byte(key))
hm.Write(val)
for _,v := range args {
hm.Write([]byte(v))
}
return fmt.Sprintf("%02x", hm.Sum(nil))
}

func Encode(data interface{}) ([]byte, error) {
//return JsonEncode(data)
return GobEncode(data)
}

func Decode(data []byte, to interface{}) error {
//return JsonDecode(data, to)
return GobDecode(data, to)
}

func GobEncode(data interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(&data)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func GobDecode(data []byte, to interface{}) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return dec.Decode(to)
}

func JsonEncode(data interface{}) ([]byte, error) {
val, err := json.Marshal(data)
if err != nil {
return nil, err
}
return val, nil
}

func JsonDecode(data []byte, to interface{}) error {
return json.Unmarshal(data, to)
}

func Base64Encode(val string) string {
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
encoder.Write([]byte(val))
encoder.Close()
return strings.TrimRight(buf.String(), "=")
}

func Base64Decode(val string) string {
buf := bytes.NewBufferString(val)
encoder := base64.NewDecoder(base64.StdEncoding, buf)
res, _ := ioutil.ReadAll(encoder)
return string(res)
}

0 comments on commit 22da862

Please sign in to comment.