You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. securecookie
  2. ============
  3. [![GoDoc](https://godoc.org/github.com/gorilla/securecookie?status.svg)](https://godoc.org/github.com/gorilla/securecookie) [![Build Status](https://travis-ci.org/gorilla/securecookie.png?branch=master)](https://travis-ci.org/gorilla/securecookie)
  4. securecookie encodes and decodes authenticated and optionally encrypted
  5. cookie values.
  6. Secure cookies can't be forged, because their values are validated using HMAC.
  7. When encrypted, the content is also inaccessible to malicious eyes. It is still
  8. recommended that sensitive data not be stored in cookies, and that HTTPS be used
  9. to prevent cookie [replay attacks](https://en.wikipedia.org/wiki/Replay_attack).
  10. ## Examples
  11. To use it, first create a new SecureCookie instance:
  12. ```go
  13. // Hash keys should be at least 32 bytes long
  14. var hashKey = []byte("very-secret")
  15. // Block keys should be 16 bytes (AES-128) or 32 bytes (AES-256) long.
  16. // Shorter keys may weaken the encryption used.
  17. var blockKey = []byte("a-lot-secret")
  18. var s = securecookie.New(hashKey, blockKey)
  19. ```
  20. The hashKey is required, used to authenticate the cookie value using HMAC.
  21. It is recommended to use a key with 32 or 64 bytes.
  22. The blockKey is optional, used to encrypt the cookie value -- set it to nil
  23. to not use encryption. If set, the length must correspond to the block size
  24. of the encryption algorithm. For AES, used by default, valid lengths are
  25. 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
  26. Strong keys can be created using the convenience function GenerateRandomKey().
  27. Once a SecureCookie instance is set, use it to encode a cookie value:
  28. ```go
  29. func SetCookieHandler(w http.ResponseWriter, r *http.Request) {
  30. value := map[string]string{
  31. "foo": "bar",
  32. }
  33. if encoded, err := s.Encode("cookie-name", value); err == nil {
  34. cookie := &http.Cookie{
  35. Name: "cookie-name",
  36. Value: encoded,
  37. Path: "/",
  38. }
  39. http.SetCookie(w, cookie)
  40. }
  41. }
  42. ```
  43. Later, use the same SecureCookie instance to decode and validate a cookie
  44. value:
  45. ```go
  46. func ReadCookieHandler(w http.ResponseWriter, r *http.Request) {
  47. if cookie, err := r.Cookie("cookie-name"); err == nil {
  48. value := make(map[string]string)
  49. if err = s2.Decode("cookie-name", cookie.Value, &value); err == nil {
  50. fmt.Fprintf(w, "The value of foo is %q", value["foo"])
  51. }
  52. }
  53. }
  54. ```
  55. We stored a map[string]string, but secure cookies can hold any value that
  56. can be encoded using `encoding/gob`. To store custom types, they must be
  57. registered first using gob.Register(). For basic types this is not needed;
  58. it works out of the box. An optional JSON encoder that uses `encoding/json` is
  59. available for types compatible with JSON.
  60. ## License
  61. BSD licensed. See the LICENSE file for details.