Skip to content

Commit 5050331

Browse files
authored
Merge pull request #38 from nhooyr/impl-1
Mask Implementation
2 parents b4ce02e + 00b9917 commit 5050331

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func main() {
6464
}
6565
```
6666

67-
For a production quality example that shows off the low level API, see the [echo example](https://godoc.org/nhooyr.io/websocket#ex-Accept--Echo).
67+
For a production quality example that shows off the low level API, see the echo example on the [godoc](https://godoc.org/nhooyr.io/websocket#Accept).
6868

6969
### Client
7070

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module nhooyr.io/websocket
33
go 1.12
44

55
require (
6+
github.com/google/go-cmp v0.2.0
67
github.com/kr/pretty v0.1.0 // indirect
78
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16
89
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
2+
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
13
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
24
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
35
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

mask.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package websocket
22

3-
// Mask applies the WebSocket masking algorithm to p
3+
// mask applies the WebSocket masking algorithm to p
44
// with the given key where the first 3 bits of pos
55
// are the starting position in the key.
66
// See https://tools.ietf.org/html/rfc6455#section-5.3
77
//
8-
// It is highly optimized to mask per word with the usage
9-
// of unsafe.
10-
//
11-
// For targets that do not support unsafe, please report an issue.
12-
// There is a mask by byte function below that will be used for such targets.
8+
// The returned value is the position of the next byte
9+
// to be used for masking in the key. This is so that
10+
// unmasking can be performed without the entire frame.
1311
func mask(key [4]byte, pos int, p []byte) int {
14-
panic("TODO")
12+
for i := range p {
13+
p[i] ^= key[pos&3]
14+
pos++
15+
}
16+
return pos & 3
1517
}

mask_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package websocket
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func Test_mask(t *testing.T) {
10+
t.Parallel()
11+
12+
key := [4]byte{0xa, 0xb, 0xc, 0xff}
13+
p := []byte{0xa, 0xb, 0xc, 0xf2, 0xc}
14+
pos := 0
15+
pos = mask(key, pos, p)
16+
17+
if exp := []byte{0, 0, 0, 0x0d, 0x6}; !cmp.Equal(exp, p) {
18+
t.Fatalf("unexpected mask: %v", cmp.Diff(exp, p))
19+
}
20+
21+
if exp := 1; !cmp.Equal(exp, pos) {
22+
t.Fatalf("unexpected mask pos: %v", cmp.Diff(exp, pos))
23+
}
24+
}

0 commit comments

Comments
 (0)