File: util.go

package info (click to toggle)
golang-github-siddontang-go 0.0~git20170517.0.cb568a3-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 484 kB
  • sloc: makefile: 3
file content (36 lines) | stat: -rw-r--r-- 743 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package websocket

import (
	"crypto/rand"
	"crypto/sha1"
	"encoding/base64"
	"errors"
	"io"
)

var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")

func calcAcceptKey(key string) string {
	h := sha1.New()
	h.Write([]byte(key))
	h.Write(keyGUID)
	return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

func calcKey() (string, error) {
	p := make([]byte, 16)
	if _, err := io.ReadFull(rand.Reader, p); err != nil {
		return "", err
	}
	return base64.StdEncoding.EncodeToString(p), nil
}

func HandleCloseFrame(buf []byte) (int16, string, error) {

	if len(buf) < 2 {
		return 0, "", errors.New("close frame msg's length less than 2")
	}
	code := int16(buf[0])<<8 + int16(buf[1])
	reason := string(buf[2:])
	return code, reason, nil
}