github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/gorilla/websocket/util.go (about)

     1  // Copyright 2013 The Gorilla WebSocket Authors. 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  
     5  package websocket
     6  
     7  import (
     8  	"crypto/rand"
     9  	"crypto/sha1"
    10  	"encoding/base64"
    11  	"io"
    12  	"net/http"
    13  	"strings"
    14  )
    15  
    16  // tokenListContainsValue returns true if the 1#token header with the given
    17  // name contains token.
    18  func tokenListContainsValue(header http.Header, name string, value string) bool {
    19  	for _, v := range header[name] {
    20  		for _, s := range strings.Split(v, ",") {
    21  			if strings.EqualFold(value, strings.TrimSpace(s)) {
    22  				return true
    23  			}
    24  		}
    25  	}
    26  	return false
    27  }
    28  
    29  var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
    30  
    31  func computeAcceptKey(challengeKey string) string {
    32  	h := sha1.New()
    33  	h.Write([]byte(challengeKey))
    34  	h.Write(keyGUID)
    35  	return base64.StdEncoding.EncodeToString(h.Sum(nil))
    36  }
    37  
    38  func generateChallengeKey() (string, error) {
    39  	p := make([]byte, 16)
    40  	if _, err := io.ReadFull(rand.Reader, p); err != nil {
    41  		return "", err
    42  	}
    43  	return base64.StdEncoding.EncodeToString(p), nil
    44  }