public string GetAttributeKeys(MessageSTUN message) { string attrKeys = ""; foreach (KeyValuePair <STUNAttribute, object> entry in message.response) { string key = Enum.GetName(typeof(STUNAttribute), entry.Key); int id = (int)entry.Key; if (attrKeys.Length > 0) { attrKeys += "\n"; } object value = entry.Value; string valueStr = ""; if (value is string v) { valueStr = v; } else if (value is byte[] b) { valueStr = NetworkSerializer.ByteArrayToHexString(b); } else { valueStr = value.ToString(); } attrKeys += key + "(" + id.ToString("X") + ") = " + valueStr; } return(attrKeys); }
public void AddMessageIntegrity(NetworkPacket packet) { string saslPassword = new SASLprep().Prepare(password); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string valueToHashMD5 = string.Format("{0}:{1}:{2}", username, realm, saslPassword); byte[] hmacSha1Key = md5.ComputeHash(Encoding.UTF8.GetBytes(valueToHashMD5)); HMACSHA1 hmacSha1 = new HMACSHA1(hmacSha1Key); byte[] hmacBytes = hmacSha1.ComputeHash(packet.ByteBuffer, 0, packet.byteLength); packet.Write((ushort)STUNAttribute.MessageIntegrity); packet.Write((ushort)hmacBytes.Length); packet.Write(hmacBytes); Console.WriteLine("Write Attribute: MessageIntegrity(2) MD5 = " + NetworkSerializer.ByteArrayToHexString(hmacSha1Key)); Console.WriteLine("Write Attribute: MessageIntegrity(2) HMAC = " + NetworkSerializer.ByteArrayToHexString(hmacBytes)); }
public void LogAttribute(int index) { STUNAttribute attr = attributeTypes[index]; string valueStr = ""; if (response.ContainsKey(attr)) { object value = response[attr]; if (value is string v) { valueStr = v; } else if (value is byte[] b) { valueStr = NetworkSerializer.ByteArrayToHexString(b); } else { valueStr = value.ToString(); } } Console.WriteLine("Write Attribute: " + Enum.GetName(typeof(STUNAttribute), attr) + " = " + valueStr); }
public void WriteBytes(STUNAttribute attr, byte[] bytes) { serializer.SetBufferLength(0); serializer.Write((ushort)attr); serializer.Write((ushort)bytes.Length); serializer.Write(bytes); //pad to multiple of 4 PadTo32Bits(bytes.Length, serializer); Console.WriteLine("Attribute: " + Enum.GetName(typeof(STUNAttribute), attr) + " = " + NetworkSerializer.ByteArrayToHexString((byte[])bytes)); response.Add(attr, bytes); attributeTypes.Add(attr); attributeBytes.Add(serializer.ToArray()); }