If you write your own WebSocket server, put one behind a custom proxy, or run one on a microcontroller, the first bug you are likely to hit is the handshake. The browser sends a Sec-WebSocket-Key. Your server answers with a Sec-WebSocket-Accept. If that answer is off by even one character, the connection never opens. Chromium's handshake code reports it as Incorrect 'Sec-WebSocket-Accept' header value.

The rule takes one line, but there are a few ways to get it subtly wrong. Below we compute it by hand, reproduce four common mistakes with real output, and look at a known erratum in the RFC's own example.

All values below come from the RFC's published example or from synthetic keys we generated. All outputs were produced by running the code shown in Node.js 24, and cross-checked in Python 3.

The rule (RFC 6455)

  1. Take the Sec-WebSocket-Key header value as a string.
  2. Append the fixed GUID 258EAFA5-E914-47DA-95CA-C5AB0DC85B11.
  3. SHA-1 the result. You get 20 raw bytes.
  4. Base64-encode those 20 bytes (standard alphabet, with padding).
import crypto from 'node:crypto'; const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; const accept = key => crypto.createHash('sha1').update(key + GUID).digest('base64'); console.log(accept('dGhlIHNhbXBsZSBub25jZQ==')); // s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 

That is exactly the value printed in RFC 6455, section 1.3.

Walking through the example

The key dGhlIHNhbXBsZSBub25jZQ== is 24 characters long. Decoded, it is 16 bytes:

74 68 65 20 73 61 6D 70 6C 65 20 6E 6F 6E 63 65 -> "the sample nonce" 

The RFC requires the key to be a base64-encoded random 16-byte nonce (section 4.1). The server does not need to decode it, though. It only needs to hash the text:

dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11 

SHA-1 of that string, in hex (40 characters = 20 bytes):

b37a4f2cc0624f1690f64606cf385945b2bec4ea 

The same 20 bytes in Base64 (28 characters, one =):

s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 

Four mistakes and what they produce

Same RFC key, four broken implementations:

Mistake What the server sends
Base64 of the hex string instead of the raw digest YjM3YTRmMmNjMDYyNGYxNjkwZjY0NjA2Y2YzODU5NDViMmJlYzRlYQ== (56 chars)
Decoding the key first, then hashing bytes + GUID BXhwxM0SZasKmCD9S4BfpPmCnEw=
A trailing space left on the key by a hand-written header parser xytUlkF2rVz2XzwONMCug9nn1qA=
GUID typed in lowercase WJfwerbIozKsJXHxxI0JTO1SQgU=

The first one is the easiest to spot: a correct accept value is always 28 characters and ends in a single =. A 56-character value starting with YjM3 means you encoded the text "b37a..." rather than the bytes. In Node that usually comes from digest('hex') followed by Buffer.from(hex).toString('base64'). Without the second argument, Buffer.from treats the hex as UTF-8 text. Either call digest('base64') directly or use Buffer.from(hex, 'hex').

The other three give a 28-character value that looks fine, which is why they take longer to find.

Does the client really reject it?

We ran a tiny http server that answers the upgrade event with one of three accept values, and connected to it with Node 24's built-in WebSocket client:

// modes = { good, hexThenB64, decodeFirst }: one accept function each srv.on('upgrade', (req, sock) => { const mode = req.url.slice(1); // e.g. ws://127.0.0.1:PORT/good const key = req.headers['sec-websocket-key']; sock.write('HTTP/1.1 101 Switching Protocols\r\n' + 'Upgrade: websocket\r\nConnection: Upgrade\r\n' + `Sec-WebSocket-Accept: ${modes[mode](key)}\r\n\r\n`); }); 

Result: the correct value fired open. The hex-then-Base64 value and the decode-first value both fired error, and the connection never opened. The client generated a fresh 16-byte key each time, as the RFC requires.

The RFC example that ends in the wrong letter

RFC 6455 section 4.1 also gives an example key: the bytes 0x01 to 0x10 should be sent as AQIDBAUGBwgJCgsMDQ4PEC==.

That is not what a correct encoder produces. Encode those 16 bytes yourself and you get AQIDBAUGBwgJCgsMDQ4PEA==, ending in A, not C. The RFC Editor lists this as verified Errata ID 3150: the example was made by an encoder that did not set the unused padding bits to zero.

Why does this matter for a server?

AQIDBAUGBwgJCgsMDQ4PEC== -> bytes 01 02 ... 0F 10 -> accept OfS0wDaT5NoxF2gqm7Zj2YtetzM= AQIDBAUGBwgJCgsMDQ4PEA== -> bytes 01 02 ... 0F 10 -> accept C/0nmHhBztSRGR1CwL6Tf4ZjwpY= 

Both strings decode to the same 16 bytes in lenient decoders (browser atob, Node Buffer, Python base64.b64decode). But they are different strings, so they hash to different accept values. A server that "normalises" the key by decoding and re-encoding it would send C/0n… to a client that sent …PEC== and expects OfS0…. The fix is the rule itself: hash the exact header text you received, trimmed of surrounding whitespace, and nothing else.

One more sanity check that follows from the errata note. A 16-byte key always ends in two =, and the character before them can only be A, Q, g or w. We generated 20,000 random keys with crypto.randomBytes(16) and saw exactly those four endings. If you log a key that ends in some other letter, the client's encoder is non-standard. That is fine as long as you hash the string as received.

Checking the bytes in a browser tool

If you have a hex digest from a log or a sha1sum and want to check the Base64 form without writing code, the Hex ↔ Base64 page does it in the browser:

  1. Choose Hex to Base64 and paste the 40-character SHA-1 hex into the Hex String Input box. Spaces or colons between bytes are fine. The Base64 Output updates as you type and should be 28 characters long.
  2. Click Swap Inputs (or choose Base64 to Hex) and paste a Sec-WebSocket-Key. The Hexadecimal Output should show 16 bytes. For AQIDBAUGBwgJCgsMDQ4PEC== it shows 01 02 … 0F 10, the same bytes as the corrected …PEA==.

One tip from testing: paste only the hex digits. The converter drops every non-hex character. A prefix like 0x triggers an odd-length error, and a prefix like sha1= quietly leaves its a and 1 in the input, which changes the output.

Tool: https://base64decodetool.com/hex/

Disclosure: this tool is from Mind vs Drive, the team behind this account.

Remember

Sec-WebSocket-Accept only proves the server understood a WebSocket handshake. It is not authentication, and Base64 is not encryption. Check identity separately, for example with a token or cookie validated on upgrade and an Origin check.

Sources

The Mind vs Drive team