Skip to content

Instantly share code, notes, and snippets.

@przemoc
Last active February 6, 2018 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save przemoc/6146235 to your computer and use it in GitHub Desktop.
Save przemoc/6146235 to your computer and use it in GitHub Desktop.
Shell script for calculating Sec-WebSocket-Accept from Sec-WebSocket-Key
#!/bin/sh
# SPDX-License-Identifier: MIT
## Copyright (C) 2013 Przemyslaw Pawelczyk <przemoc@gmail.com>
##
## This script is licensed under the terms of the MIT license.
## https://opensource.org/licenses/MIT
_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
@przemoc
Copy link
Author

przemoc commented Feb 6, 2018

commit 78adfbacbaa9dc465f82d560c3ee678a22fa2c68
Author: Przemyslaw Pawelczyk <przemoc@gmail.com>
Date:   2018-02-06 14:48:41 +0100

    calcswsa.sh: Add copyright notice and MIT license notice.

commit 7b11ae6c4e0bdbd386bbe82e0a6b03bb5f4dcfcc
Author: Przemyslaw Pawelczyk <przemoc@gmail.com>
Date:   2018-02-06 14:49:23 +0100

    Add SPDX License Identifier.

    The Software Package Data Exchange (SPDX) is a good initiative, it has
    matured over time and deserves accelerated adoption in open-source.

    https://spdx.org/learn
    https://spdx.org/using-spdx
    https://spdx.org/license-list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment