Created
January 18, 2012 20:13
-
-
Save chocoboba/1635265 to your computer and use it in GitHub Desktop.
HybiEncode
This file contains 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 byte[] HybiEncode(string message) | |
{ | |
if (message.Length > 125) | |
{ | |
message = message.Substring(0, 125); | |
} | |
byte[] payload = Encoding.UTF8.GetBytes(message); | |
List<int> frameHead = new List<int>(); | |
int payloadLength = payload.Length; | |
frameHead.Add(129); // 1000 0001 FIN and opcode of text | |
frameHead.Add(payloadLength + 128); // 128 for masking | |
Random random = new Random(); | |
List<int> mask = new List<int>(); | |
for(int i = 0; i < 4; i++) | |
{ | |
mask.Add(random.Next(0, 255)); | |
} | |
frameHead.AddRange(mask); | |
for (int i = 0; i < payloadLength; i++) | |
{ | |
frameHead.Add(payload[i] ^ mask[i % 4]); | |
} | |
return frameHead.Select(e => (byte)e).ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment