diff options
-rw-r--r-- | securecookie.go | 38 | ||||
-rw-r--r-- | securecookie_test.go | 12 |
2 files changed, 25 insertions, 25 deletions
diff --git a/securecookie.go b/securecookie.go index 8ed512a..cfee1d8 100644 --- a/securecookie.go +++ b/securecookie.go @@ -53,7 +53,7 @@ func New(hashKey, blockKey []byte) *SecureCookie { hashFunc: sha256.New, maxAge: 86400 * 30, maxLength: 4096, - enc: GobEncoder{}, + sz: GobEncoder{}, } if hashKey == nil { s.err = errHashKeyNotSet @@ -75,17 +75,17 @@ type SecureCookie struct { maxAge int64 minAge int64 err error - enc encoder + sz Serializer // For testing purposes, the function that returns the current timestamp. // If not set, it will use time.Now().UTC().Unix(). timeFunc func() int64 } -// Encoder provides an interface for providing custom encoders for cookie +// Serializer provides an interface for providing custom serializers for cookie // values. -type encoder interface { - serialize(src interface{}) ([]byte, error) - deserialize(src []byte, dst interface{}) error +type Serializer interface { + Serialize(src interface{}) ([]byte, error) + Deserialize(src []byte, dst interface{}) error } // GobEncoder encodes cookie values using encoding/gob. This is the simplest @@ -146,8 +146,8 @@ func (s *SecureCookie) BlockFunc(f func([]byte) (cipher.Block, error)) *SecureCo // Encoding sets the encoding/serialization method for cookies. // // Default is encoding/gob. -func (s *SecureCookie) Encoding(e encoder) *SecureCookie { - s.enc = e +func (s *SecureCookie) SetSerializer(sz Serializer) *SecureCookie { + s.sz = sz return s } @@ -172,7 +172,7 @@ func (s *SecureCookie) Encode(name string, value interface{}) (string, error) { var err error var b []byte // 1. Serialize. - if b, err = s.enc.serialize(value); err != nil { + if b, err = s.sz.Serialize(value); err != nil { return "", err } // 2. Encrypt (optional). @@ -255,7 +255,7 @@ func (s *SecureCookie) Decode(name, value string, dst interface{}) error { } } // 6. Deserialize. - if err = s.enc.deserialize(b, dst); err != nil { + if err = s.sz.Deserialize(b, dst); err != nil { return err } // Done. @@ -329,8 +329,8 @@ func decrypt(block cipher.Block, value []byte) ([]byte, error) { // Serialization -------------------------------------------------------------- -// serialize encodes a value using gob. -func (e GobEncoder) serialize(src interface{}) ([]byte, error) { +// Serialize encodes a value using gob. +func (e GobEncoder) Serialize(src interface{}) ([]byte, error) { buf := new(bytes.Buffer) enc := gob.NewEncoder(buf) if err := enc.Encode(src); err != nil { @@ -339,8 +339,8 @@ func (e GobEncoder) serialize(src interface{}) ([]byte, error) { return buf.Bytes(), nil } -// deserialize decodes a value using gob. -func (e GobEncoder) deserialize(src []byte, dst interface{}) error { +// Deserialize decodes a value using gob. +func (e GobEncoder) Deserialize(src []byte, dst interface{}) error { dec := gob.NewDecoder(bytes.NewBuffer(src)) if err := dec.Decode(dst); err != nil { return err @@ -348,8 +348,8 @@ func (e GobEncoder) deserialize(src []byte, dst interface{}) error { return nil } -// serialize encodes a value using encoding/json. -func (e JSONEncoder) serialize(src interface{}) ([]byte, error) { +// Serialize encodes a value using encoding/json. +func (e JSONEncoder) Serialize(src interface{}) ([]byte, error) { buf := new(bytes.Buffer) enc := json.NewEncoder(buf) if err := enc.Encode(src); err != nil { @@ -359,9 +359,9 @@ func (e JSONEncoder) serialize(src interface{}) ([]byte, error) { return buf.Bytes(), nil } -// deserialize decodes a value using encoding/json. -func (e JSONEncoder) deserialize(src []byte, dst interface{}) error { - dec := json.NewDecoder(bytes.NewBuffer(src)) +// Deserialize decodes a value using encoding/json. +func (e JSONEncoder) Deserialize(src []byte, dst interface{}) error { + dec := json.NewDecoder(bytes.NewReader(src)) if err := dec.Decode(dst); err != nil { return err } diff --git a/securecookie_test.go b/securecookie_test.go index 8b984ee..76368a9 100644 --- a/securecookie_test.go +++ b/securecookie_test.go @@ -123,17 +123,17 @@ func TestEncryption(t *testing.T) { func TestGobSerialization(t *testing.T) { var ( - enc GobEncoder + sz GobEncoder serialized []byte deserialized map[string]string err error ) for _, value := range testCookies { - if serialized, err = enc.serialize(value); err != nil { + if serialized, err = sz.Serialize(value); err != nil { t.Error(err) } else { deserialized = make(map[string]string) - if err = enc.deserialize(serialized, &deserialized); err != nil { + if err = sz.Deserialize(serialized, &deserialized); err != nil { t.Error(err) } if fmt.Sprintf("%v", deserialized) != fmt.Sprintf("%v", value) { @@ -145,17 +145,17 @@ func TestGobSerialization(t *testing.T) { func TestJSONSerialization(t *testing.T) { var ( - enc JSONEncoder + sz JSONEncoder serialized []byte deserialized map[string]string err error ) for _, value := range testCookies { - if serialized, err = enc.serialize(value); err != nil { + if serialized, err = sz.Serialize(value); err != nil { t.Error(err) } else { deserialized = make(map[string]string) - if err = enc.deserialize(serialized, &deserialized); err != nil { + if err = sz.Deserialize(serialized, &deserialized); err != nil { t.Error(err) } if fmt.Sprintf("%v", deserialized) != fmt.Sprintf("%v", value) { |