util.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2013 Gary Burd. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package websocket
  5. import (
  6. "crypto/rand"
  7. "crypto/sha1"
  8. "encoding/base64"
  9. "io"
  10. "net/http"
  11. "strings"
  12. )
  13. // tokenListContainsValue returns true if the 1#token header with the given
  14. // name contains token.
  15. func tokenListContainsValue(header http.Header, name string, value string) bool {
  16. for _, v := range header[name] {
  17. for _, s := range strings.Split(v, ",") {
  18. if strings.EqualFold(value, strings.TrimSpace(s)) {
  19. return true
  20. }
  21. }
  22. }
  23. return false
  24. }
  25. var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
  26. func computeAcceptKey(challengeKey string) string {
  27. h := sha1.New()
  28. h.Write([]byte(challengeKey))
  29. h.Write(keyGUID)
  30. return base64.StdEncoding.EncodeToString(h.Sum(nil))
  31. }
  32. func generateChallengeKey() (string, error) {
  33. p := make([]byte, 16)
  34. if _, err := io.ReadFull(rand.Reader, p); err != nil {
  35. return "", err
  36. }
  37. return base64.StdEncoding.EncodeToString(p), nil
  38. }