Created
January 18, 2012 19:43
-
-
Save joydrinkstea/1635108 to your computer and use it in GitHub Desktop.
HybiDecode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public string HybiDecode(byte[] data) | |
| { | |
| byte firstByte = data[0]; | |
| byte secondByte = data[1]; | |
| int opcode = firstByte & 0x0F; | |
| bool isMasked = ((firstByte & 128) == 128); | |
| int payloadLength = secondByte & 0x7F; | |
| if (!isMasked) { return null; } // not masked | |
| if (opcode != 1) { return null; } // not text | |
| List<int> mask = new List<int>(); | |
| for (int i = 2; i < 6; i++) | |
| { | |
| mask.Add(data[i]); | |
| } | |
| int payloadOffset = 6; | |
| int dataLength = payloadLength + payloadOffset; | |
| List<int> unmaskedPayload = new List<int>(); | |
| for (int i = payloadOffset; i < dataLength; i++) | |
| { | |
| int j = i - payloadOffset; | |
| unmaskedPayload.Add(data[i] ^ mask[j % 4]); | |
| } | |
| return ToAscii(unmaskedPayload.Select(e => (byte)e).ToArray()); | |
| } | |
| public string ToAscii(byte[] data) | |
| { | |
| System.Text.ASCIIEncoding decoder = new System.Text.ASCIIEncoding(); | |
| return decoder.GetString(data, 0, data.Length); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment