public static byte[] AddSignatureAndMac(byte[] bytes, string sharedSecret) { TimeSpan sinceMidnight = DateTime.Now - DateTime.Today; int timez = (int)sinceMidnight.TotalMilliseconds; bytes = ByteHelper.ConcatinateArray(BitConverter.GetBytes(timez), bytes); // Add a milisecond timestamp to the meassage. byte[] macBytes = Convert.FromBase64String(CreateMac(bytes, sharedSecret)); byte[] singatureBytes = new byte[] { 0x92, _version }; // Signature byte and version byte. bytes = ByteHelper.ConcatinateArray(singatureBytes, macBytes, bytes); return(bytes); }
public static byte[] MessageTransmissionHandler(Message message) { byte[] textBytes = new byte[0]; if (!String.IsNullOrEmpty(message.Text)) { textBytes = Encoding.Unicode.GetBytes(message.Text); } byte[] dataBytes = new byte[0]; if (message.Data != null) { dataBytes = message.Data; } byte[] messageBytes = ByteHelper.ConcatinateArray(BitConverter.GetBytes(message.From), new byte[] { (byte)message.Type }); messageBytes = ByteHelper.ConcatinateArray(messageBytes, dataBytes, textBytes); return(messageBytes); }
private static void Keepalive() // Threaded looping method. { try { while (_loggedIn) { Thread.Sleep(500); List <ContactObj> onlineContacts = _contacts.FindAll(x => x.Online == true); byte[] contactIds = new byte[0]; foreach (ContactObj contact in onlineContacts) { byte[] contactId = BitConverter.GetBytes(contact.ID); contactIds = ByteHelper.ConcatinateArray(contactIds, contactId); } ChatTwo_Client_Protocol.MessageToServer(ChatTwo_Protocol.MessageType.Status, contactIds, null); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("### " + _threadKeepalive.Name + " has crashed:"); System.Diagnostics.Debug.WriteLine("### " + ex.Message); System.Diagnostics.Debug.WriteLine("### " + ex.ToString()); } }
private static string CreateMac(byte[] messageBytes, string sharedSecret) { return(ByteHelper.GetHashString(ByteHelper.ConcatinateArray(ByteHelper.GetHashBytes(messageBytes), Convert.FromBase64String(sharedSecret)))); }
/// <summary> /// Create an ACK packet from a base64 hash string. /// </summary> /// <param name="hash">Base64 hash string to be used.</param> protected byte[] CreateAck(string hash) { byte[] ackTag = new byte[] { 0xCE }; // 0xCE = 206 byte[] ackBytes = ByteHelper.ConcatinateArray(ackTag, Convert.FromBase64String(hash), ackTag); return(ackBytes); }