0

Currently I'm working on project that involves WebSocket technology (Node.js server). And I have a really common problem (there is tons of similar questions on StackOverflow). My Sec-WebSocket-Accept is not accepted by browser. And it looks like base64 encoding fails. Example:

Sec-WebSocket-Key: Hel69aX+iCF5/Js4bATfRw==
Magic number appended: Hel69aX+iCF5/Js4bATfRw==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
SHA-1: b549e61a292a87ce1110b880a0792de41e2d0803
base64: YjU0OWU2MWEyOTJhODdjZTExMTBiODgwYTA3OTJkZTQxZTJkMDgwMw==

Thought my script issues wrong conversion, but this values are being returned by any encoders (base64, sha1) I've tried.

Next I've tried to look some step-by-step Sec-WebSocket-Accept examples, but their base64 differs from what I get. They are actually much shorter.

For example this one.

base64('e53e4cbf13f58b3e34bcba62de442cfe27c3682a') returns 5T5MvxP1iz40vLpi3kQs/ifDaCo= in that thread. I've tried browser's btoa('e53e4cbf13f58b3e34bcba62de442cfe27c3682a') and some online base64 encoders, and all of them returns 'ZTUzZTRjYmYxM2Y1OGIzZTM0YmNiYTYyZGU0NDJjZmUyN2MzNjgyYQ=='.

What am I missing?

Thank you!

SOLUTION UPDATE: Question marked as a duplicate as original question explains the reason of my issue. But real solution provided by gre_gor in comments section.

Community
  • 1
  • 1
MadBrozzeR
  • 341
  • 3
  • 15
  • For Node.js you should calculate the `Sec-WebSocket-Accept` header as in [this example](http://ideone.com/Gqy1ak). – gre_gor Nov 07 '16 at 14:21
  • Also why are you implementing WebSocket on your own? There are already existing implementations of WebSockets for Node.js. – gre_gor Nov 07 '16 at 14:23
  • @gre_gor looks like duplicate, it is. Failed to find it, unfortunately. And according to that thread now I know the reason. Now I'll try to use your example to solve it. – MadBrozzeR Nov 07 '16 at 14:48
  • @gre_gor, I always forget to mention... for my own projects I try to avoid external libraries, and write my own for learning purposes, and to see all algorithms "from the inside". – MadBrozzeR Nov 07 '16 at 14:52
  • @gre_gor, worked! Thank you! But now I don't really understand what to do with my question. It feels like duplicate, but original question didn't solve my problem, only explained the reason. Your solution helped. If you'll post an answer, I'll check it as a solution. If you'll choose not to, then I'll just close it as a duplicate. – MadBrozzeR Nov 07 '16 at 15:41
  • I would say it's a duplicate, since it's the same problem of using a hex string of the digest. And if the answer in the original helped you, you should upvote it. – gre_gor Nov 08 '16 at 00:52

0 Answers0