Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Shell script for calculating Sec-WebSocket-Accept from Sec-WebSocket-Key
#!/bin/sh
_calcswsa()
{
echo -n "$1"'258EAFA5-E914-47DA-95CA-C5AB0DC85B11' | sha1sum | awk '{print$1}' | xxd -r -p | base64
}
if [ -t 0 -a -z "$1" ]; then
echo "usage: $0 [-] [SEC_WEBSOCKET_KEY]..."
echo
echo "For each given Sec-WebSocket-Key (provided on the standard input or as a command-line argument; in that order) returns Sec-WebSocket-Accept in a new line."
fi
if [ "$1" = "-" -o ! -t 0 ]; then
while read -r LINE; do
for KEY in $LINE; do
_calcswsa "$KEY"
done
done
if [ "$1" = "-" ]; then
shift
fi
fi
while [ -n "$1" ]; do
_calcswsa "$1"
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment