Last active
November 16, 2015 15:56
something important about Websocket handshaking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/sha1' | |
require 'base64' | |
# client request | |
# GET / HTTP/1.1 | |
# Upgrade: websocket | |
# Connection: Upgrade | |
# Host: example.com | |
# Origin: null | |
# Sec-WebSocket-Key: sN9cRrP/n9NdMgdcy2VJFQ== | |
# Sec-WebSocket-Version: 13 | |
# server response | |
# HTTP/1.1 101 Switching Protocols | |
# Upgrade: websocket | |
# Connection: Upgrade | |
# Sec-WebSocket-Accept: fFBooB7FAkLlXgRSz0BT3v4hq5s= | |
# Sec-WebSocket-Origin: null | |
# Sec-WebSocket-Location: ws://example.com/ | |
sec_websoekct_key = 'sN9cRrP/n9NdMgdcy2VJFQ==' | |
p "sec_websoekct_key is #{sec_websoekct_key}" | |
token = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' | |
# Digest::SHA1.base64digest(sec_websoekct_key + token) | |
# NOTICE!! hexdigest is invalid. | |
encrypted = Digest::SHA1.digest(sec_websoekct_key + token) | |
sec_websokect_accept = Base64.encode64(encrypted).strip | |
p "generated sec_websokect_accept is #{sec_websokect_accept}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment