Skip to content

Instantly share code, notes, and snippets.

@belm0
Created June 15, 2019 12:41
Show Gist options
  • Save belm0/2b610cc405dd3dac977f34650cb187cc to your computer and use it in GitHub Desktop.
Save belm0/2b610cc405dd3dac977f34650cb187cc to your computer and use it in GitHub Desktop.
websocket data masking implementations
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"websocket data masking implementations from https://www.willmcgugan.com/blog/tech/post/speeding-up-websockets-60x/"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"from itertools import cycle\n",
"def mask_cycle(mask, data):\n",
" return bytes(a ^ b for a, b in zip(cycle(mask), data))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"from wsaccel.xormask import XorMaskerSimple\n",
"def mask_wsaccel(mask, data):\n",
" return XorMaskerSimple(mask).process(data)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"from sys import byteorder as native_byteorder\n",
"def mask_from_bytes(mask, data):\n",
" datalen = len(data)\n",
" data = int.from_bytes(data, native_byteorder)\n",
" mask = int.from_bytes(mask * (datalen // 4) + mask[: datalen % 4], native_byteorder)\n",
" return (data ^ mask).to_bytes(datalen, native_byteorder)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"_XOR_TABLE = [bytes(a ^ b for a in range(256)) for b in range(256)]\n",
"def mask_translate(mask, data):\n",
" data_bytes = bytearray(data)\n",
" a, b, c, d = (_XOR_TABLE[n] for n in mask)\n",
" data_bytes[::4] = data_bytes[::4].translate(a)\n",
" data_bytes[1::4] = data_bytes[1::4].translate(b)\n",
" data_bytes[2::4] = data_bytes[2::4].translate(c)\n",
" data_bytes[3::4] = data_bytes[3::4].translate(d)\n",
" return bytes(data_bytes)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.28 ms ± 18.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
"41.7 µs ± 842 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n",
"17.9 µs ± 1.2 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
"20.6 µs ± 599 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"import os\n",
"data = os.urandom(10 * 1024)\n",
"mask = os.urandom(4)\n",
"%timeit mask_cycle(mask, data)\n",
"%timeit mask_from_bytes(mask, data)\n",
"%timeit mask_translate(mask, data)\n",
"%timeit mask_wsaccel(mask, data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0b3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment