Decode a Websocket mask2019 Community Moderator ElectionWhat browsers support HTML5 WebSocket API?WebSockets vs. Server-Sent events/EventSourceDebugging WebSocket in Google ChromeDifferences between socket.io and websocketsWhy use AJAX when WebSockets is available?What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?WebSockets protocol vs HTTPWhich websocket library to use with Node.js?How do I capture utf-8 decode errors in node.js?Websocket: Javascript client not acknowledging receipt of a data frame sent from a C#. Handshake fully works, client can send messages to server
Rejected in 4th interview round citing insufficient years of experience
Silly Sally's Movie
Should QA ask requirements to developers?
Excess Zinc in garden soil
Why don't MCU characters ever seem to have language issues?
Good allowance savings plan?
Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?
Making a sword in the stone, in a medieval world without magic
When is a batch class instantiated when you schedule it?
How can I discourage/prevent PCs from using door choke-points?
Question about partial fractions with irreducible quadratic factors
"However" used in a conditional clause?
Format picture and text with TikZ and minipage
Is it true that real estate prices mainly go up?
Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?
How to make readers know that my work has used a hidden constraint?
How could a female member of a species produce eggs unto death?
Are there situations where a child is permitted to refer to their parent by their first name?
Is "history" a male-biased word ("his+story")?
what does the apostrophe mean in this notation?
If Invisibility ends because the original caster casts a non-concentration spell, does Invisibility also end on other targets of the original casting?
This equation is outside the page, how to modify it
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
Decoding assembly instructions in a Game Boy disassembler
Decode a Websocket mask
2019 Community Moderator ElectionWhat browsers support HTML5 WebSocket API?WebSockets vs. Server-Sent events/EventSourceDebugging WebSocket in Google ChromeDifferences between socket.io and websocketsWhy use AJAX when WebSockets is available?What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?WebSockets protocol vs HTTPWhich websocket library to use with Node.js?How do I capture utf-8 decode errors in node.js?Websocket: Javascript client not acknowledging receipt of a data frame sent from a C#. Handshake fully works, client can send messages to server
I'm trying to write a function to decode a masked buffer sent to a node server via a WebSocket connection. Using a combination of RFC6455 and a few old SO answers, this is what I've managed so far:
function decodeMessage(buffer)
const opCode = buffer.readUInt8(0) & 0xF;
if(opCode === 0x1)
const data_length = buffer.readUInt8(1) & 0x7F;
let decoded = Buffer.alloc(data_length);
const mask_key = buffer.readUInt32BE(2);
for (let i = 0; i < data_length; i++)
decoded[i] = buffer.readUInt8(i) ^ mask_key[i % 4];
buffer.copy(decoded, 0, 2);
return decoded.toString('utf8');
else
return null;
There's two issues with this code:
It doesn't unmask the message correctly and just outputs garbled Unicode
It outputs a different decoded message every time, even if the input is constant
(The payload length will never be longer than 126, so it doesn't need to worry about handling additional bytes)
javascript node.js websocket
add a comment |
I'm trying to write a function to decode a masked buffer sent to a node server via a WebSocket connection. Using a combination of RFC6455 and a few old SO answers, this is what I've managed so far:
function decodeMessage(buffer)
const opCode = buffer.readUInt8(0) & 0xF;
if(opCode === 0x1)
const data_length = buffer.readUInt8(1) & 0x7F;
let decoded = Buffer.alloc(data_length);
const mask_key = buffer.readUInt32BE(2);
for (let i = 0; i < data_length; i++)
decoded[i] = buffer.readUInt8(i) ^ mask_key[i % 4];
buffer.copy(decoded, 0, 2);
return decoded.toString('utf8');
else
return null;
There's two issues with this code:
It doesn't unmask the message correctly and just outputs garbled Unicode
It outputs a different decoded message every time, even if the input is constant
(The payload length will never be longer than 126, so it doesn't need to worry about handling additional bytes)
javascript node.js websocket
add a comment |
I'm trying to write a function to decode a masked buffer sent to a node server via a WebSocket connection. Using a combination of RFC6455 and a few old SO answers, this is what I've managed so far:
function decodeMessage(buffer)
const opCode = buffer.readUInt8(0) & 0xF;
if(opCode === 0x1)
const data_length = buffer.readUInt8(1) & 0x7F;
let decoded = Buffer.alloc(data_length);
const mask_key = buffer.readUInt32BE(2);
for (let i = 0; i < data_length; i++)
decoded[i] = buffer.readUInt8(i) ^ mask_key[i % 4];
buffer.copy(decoded, 0, 2);
return decoded.toString('utf8');
else
return null;
There's two issues with this code:
It doesn't unmask the message correctly and just outputs garbled Unicode
It outputs a different decoded message every time, even if the input is constant
(The payload length will never be longer than 126, so it doesn't need to worry about handling additional bytes)
javascript node.js websocket
I'm trying to write a function to decode a masked buffer sent to a node server via a WebSocket connection. Using a combination of RFC6455 and a few old SO answers, this is what I've managed so far:
function decodeMessage(buffer)
const opCode = buffer.readUInt8(0) & 0xF;
if(opCode === 0x1)
const data_length = buffer.readUInt8(1) & 0x7F;
let decoded = Buffer.alloc(data_length);
const mask_key = buffer.readUInt32BE(2);
for (let i = 0; i < data_length; i++)
decoded[i] = buffer.readUInt8(i) ^ mask_key[i % 4];
buffer.copy(decoded, 0, 2);
return decoded.toString('utf8');
else
return null;
There's two issues with this code:
It doesn't unmask the message correctly and just outputs garbled Unicode
It outputs a different decoded message every time, even if the input is constant
(The payload length will never be longer than 126, so it doesn't need to worry about handling additional bytes)
javascript node.js websocket
javascript node.js websocket
edited Mar 7 at 18:10
Jacob Barrow
asked Mar 7 at 10:35
Jacob BarrowJacob Barrow
1341316
1341316
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
By modifying a function from this post, I was able to get this working:
function decodeMessage(buffer)
if((buffer.readUInt8(0) & 0xF) === 0x1)
const length = (buffer.readUInt8(1) & 0x7F)+4;
let currentOffset = 2;
const mask_key = buffer.readUInt32BE(2);
const data = Buffer.alloc(length);
for (let i = 0, j = 0; i < length; ++i, j = i % 4)
const shift = j === 3 ? 0 : (3 - j) << 3;
const mask = (shift === 0 ? mask_key : (mask_key >>> shift)) & 0xFF;
const source = buffer.readUInt8(currentOffset++);
data.writeUInt8(mask ^ source, i);
return data.toString('utf8');
else
return null;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55041673%2fdecode-a-websocket-mask%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
By modifying a function from this post, I was able to get this working:
function decodeMessage(buffer)
if((buffer.readUInt8(0) & 0xF) === 0x1)
const length = (buffer.readUInt8(1) & 0x7F)+4;
let currentOffset = 2;
const mask_key = buffer.readUInt32BE(2);
const data = Buffer.alloc(length);
for (let i = 0, j = 0; i < length; ++i, j = i % 4)
const shift = j === 3 ? 0 : (3 - j) << 3;
const mask = (shift === 0 ? mask_key : (mask_key >>> shift)) & 0xFF;
const source = buffer.readUInt8(currentOffset++);
data.writeUInt8(mask ^ source, i);
return data.toString('utf8');
else
return null;
add a comment |
By modifying a function from this post, I was able to get this working:
function decodeMessage(buffer)
if((buffer.readUInt8(0) & 0xF) === 0x1)
const length = (buffer.readUInt8(1) & 0x7F)+4;
let currentOffset = 2;
const mask_key = buffer.readUInt32BE(2);
const data = Buffer.alloc(length);
for (let i = 0, j = 0; i < length; ++i, j = i % 4)
const shift = j === 3 ? 0 : (3 - j) << 3;
const mask = (shift === 0 ? mask_key : (mask_key >>> shift)) & 0xFF;
const source = buffer.readUInt8(currentOffset++);
data.writeUInt8(mask ^ source, i);
return data.toString('utf8');
else
return null;
add a comment |
By modifying a function from this post, I was able to get this working:
function decodeMessage(buffer)
if((buffer.readUInt8(0) & 0xF) === 0x1)
const length = (buffer.readUInt8(1) & 0x7F)+4;
let currentOffset = 2;
const mask_key = buffer.readUInt32BE(2);
const data = Buffer.alloc(length);
for (let i = 0, j = 0; i < length; ++i, j = i % 4)
const shift = j === 3 ? 0 : (3 - j) << 3;
const mask = (shift === 0 ? mask_key : (mask_key >>> shift)) & 0xFF;
const source = buffer.readUInt8(currentOffset++);
data.writeUInt8(mask ^ source, i);
return data.toString('utf8');
else
return null;
By modifying a function from this post, I was able to get this working:
function decodeMessage(buffer)
if((buffer.readUInt8(0) & 0xF) === 0x1)
const length = (buffer.readUInt8(1) & 0x7F)+4;
let currentOffset = 2;
const mask_key = buffer.readUInt32BE(2);
const data = Buffer.alloc(length);
for (let i = 0, j = 0; i < length; ++i, j = i % 4)
const shift = j === 3 ? 0 : (3 - j) << 3;
const mask = (shift === 0 ? mask_key : (mask_key >>> shift)) & 0xFF;
const source = buffer.readUInt8(currentOffset++);
data.writeUInt8(mask ^ source, i);
return data.toString('utf8');
else
return null;
answered Mar 8 at 16:06
Jacob BarrowJacob Barrow
1341316
1341316
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55041673%2fdecode-a-websocket-mask%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown