package wsimport ()const (// RFC6455: The value of this header field MUST be a nonce consisting of a // randomly selected 16-byte value that has been base64-encoded (see // Section 4 of [RFC4648]). The nonce MUST be selected randomly for each // connection. nonceKeySize = 16 nonceSize = 24// base64.StdEncoding.EncodedLen(nonceKeySize)// RFC6455: The value of this header field is constructed by concatenating // /key/, defined above in step 4 in Section 4.2.2, with the string // "258EAFA5- E914-47DA-95CA-C5AB0DC85B11", taking the SHA-1 hash of this // concatenated value to obtain a 20-byte value and base64- encoding (see // Section 4 of [RFC4648]) this 20-byte hash. acceptSize = 28// base64.StdEncoding.EncodedLen(sha1.Size))// initNonce fills given slice with random base64-encoded nonce bytes.func initNonce( []byte) {// NOTE: bts does not escape. := make([]byte, nonceKeySize)if , := rand.Read(); != nil {panic(fmt.Sprintf("rand read error: %s", )) }base64.StdEncoding.Encode(, )}// checkAcceptFromNonce reports whether given accept bytes are valid for given// nonce bytes.func checkAcceptFromNonce(, []byte) bool {iflen() != acceptSize {returnfalse }// NOTE: expect does not escape. := make([]byte, acceptSize)initAcceptFromNonce(, )returnbytes.Equal(, )}// initAcceptFromNonce fills given slice with accept bytes generated from given// nonce bytes. Given buffer should be exactly acceptSize bytes.func initAcceptFromNonce(, []byte) {const = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"iflen() != acceptSize {panic("accept buffer is invalid") }iflen() != nonceSize {panic("nonce is invalid") } := make([]byte, nonceSize+len())copy([:nonceSize], )copy([nonceSize:], ) := sha1.Sum()base64.StdEncoding.Encode(, [:])}func writeAccept( *bufio.Writer, []byte) (int, error) { := make([]byte, acceptSize)initAcceptFromNonce(, )// NOTE: write accept bytes as a string to prevent heap allocation – // WriteString() copy given string into its inner buffer, unlike Write() // which may write p directly to the underlying io.Writer – which in turn // will lead to p escape.return .WriteString(btsToString())}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.