Sec-WebSocket-Accept calculator
Paste the Sec-WebSocket-Key from the request and get the Sec-WebSocket-Accept the server must return in its 101.
Everything runs in your browser
How Sec-WebSocket-Accept is calculated
RFC 6455 § 4.2.2, step 5.4. Three steps:
- 1Take the
Sec-WebSocket-Keyvalue exactly as it arrived. Do not decode it, trim it or re-encode it. - 2Append the fixed GUID
258EAFA5-E914-47DA-95CA-C5AB0DC85B11to it. - 3Hash that ASCII string with
SHA-1and base64 the 20 bytes. The server sends the result in a101response, next toUpgrade: websocketandConnection: Upgrade.
The GUID is printed in the RFC and is the same for everyone. It shows one thing: the server really speaks WebSocket and did not just echo a header or replay a cached response.
Example: from Sec-WebSocket-Key to Sec-WebSocket-Accept
The test vector from RFC 6455 § 1.3, with every intermediate value. A server you are writing has to reproduce each line.
- Sec-WebSocket-Key
- dGhlIHNhbXBsZSBub25jZQ==
- The key with the GUID appended
- dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
- SHA-1 of that string, as hex
- b37a4f2cc0624f1690f64606cf385945b2bec4ea
- The same 20 bytes in base64: Sec-WebSocket-Accept
- s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
The handshake this pair comes from
Request
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13Response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=Sec-WebSocket-Accept in code
The same three steps in four languages. For the key dGhlIHNhbXBsZSBub25jZQ== each of them gives s3pPLMBiTxaQ9kYGzzhZRbK+xOo=.
Node.js
import { createHash } from 'node:crypto';
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
function accept(key) {
return createHash('sha1').update(key + GUID).digest('base64');
}Python
import base64
import hashlib
GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
def accept(key: str) -> str:
digest = hashlib.sha1((key + GUID).encode("ascii")).digest()
return base64.b64encode(digest).decode("ascii")Go
import (
"crypto/sha1"
"encoding/base64"
)
const guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
func accept(key string) string {
sum := sha1.Sum([]byte(key + guid))
return base64.StdEncoding.EncodeToString(sum[:])
}Shell (OpenSSL)
printf '%s' 'dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11' \
| openssl sha1 -binary | base64Questions about Sec-WebSocket-Accept
What is Sec-WebSocket-Accept?
A header in the server's 101 Switching Protocols response. It holds a SHA-1 hash of the client's Sec-WebSocket-Key and a fixed GUID, encoded in base64. The client computes the same value and compares. If the two differ, or the header is missing, the client must fail the connection. The value is always 28 characters long and ends with =.
What is Sec-WebSocket-Key?
A header the client sends in the opening GET request: 16 random bytes in base64, which is 24 characters ending in ==. The client makes a new key for every connection. In a browser a script can neither read it nor set it: headers that start with Sec- belong to the browser. With curl or a client library you can set the key yourself.
What is 258EAFA5-E914-47DA-95CA-C5AB0DC85B11?
The GUID that RFC 6455 fixes for this computation. It is the same for every server and every connection, and it is not a secret. Append it exactly as printed, in upper case. It was picked as a string that software unaware of WebSocket would not produce by accident.
What does "Incorrect 'Sec-WebSocket-Accept' header value" mean?
Chrome prints Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value when the server answered 101 but the header does not hold the expected value. The script sees only close code 1006. A hand-written server usually makes one of four mistakes: it decodes the key from base64 before hashing; it encodes the hex text of the hash instead of the 20 raw bytes, and the value comes out 56 characters long instead of 28; the GUID is mistyped or in lower case; a space or a line break is hashed together with the key. If the server computes it right, look for a proxy or a cache that returned the response to a different request.
Where do I find the key and the accept value in a browser?
Open devtools, go to the Network tab, turn on the WS filter and reload the page. Select the connection and open Headers: Sec-WebSocket-Key is among the request headers, Sec-WebSocket-Accept among the response headers. Copy both into the fields above.
Does Sec-WebSocket-Accept make a WebSocket secure?
No. The computation has no secret in it, so anyone can produce the value. That is also why the weakness of SHA-1 changes nothing here: the hash is not a signature. Encryption comes from wss://, which is TLS. The server identifies the user by a cookie or a token, and keeps other sites out by checking the Origin header.
Is Sec-WebSocket-Accept used over HTTP/2?
No. Over HTTP/2 a WebSocket opens with the extended CONNECT method (RFC 8441): the server answers 200, and neither Sec-WebSocket-Key nor Sec-WebSocket-Accept is sent. HTTP/3 works the same way (RFC 9220). Both headers exist only in the HTTP/1.1 handshake.
See also
Want to see more?
View in Solpuga