public
Created

HybiEncode

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.