public void HashAndCompare() { using (var stream = new System.IO.MemoryStream()) using (var stream2 = new System.IO.MemoryStream()) { var theObject = new TanksCommon.SharedObjects.GameMove() { MessageId = 8 }; var messageStream = TanksCommon.MessageEncoder.EncodeMessage(stream, theObject); messageStream.Seek(0, System.IO.SeekOrigin.Begin); var messageBytes = messageStream.ToArray(); var hash = GameCom.Hash.HashData(messageBytes); //slightly different object var theObject2 = new TanksCommon.SharedObjects.GameMove() { MessageId = 1 }; var messageStream2 = TanksCommon.MessageEncoder.EncodeMessage(stream2, theObject2); messageStream2.Seek(0, System.IO.SeekOrigin.Begin); var messageBytes2 = messageStream2.ToArray(); Assert.IsFalse(GameCom.Hash.HashAndCompare(messageBytes2, hash)); } }
public void MessageHashFailed() { TheMessenger.ReceivedDataDelegateForLog theHandler; TheMessenger.ReceivedDataLog += theHandler = (messageText) => { //should be asking to send message 8 //this also works if we recieve out of order, see how we send 8 Assert.IsTrue(messageText.Contains("Hash failed. Asking to resend Message: 8")); }; using (var stream = new System.IO.MemoryStream()) { var theObject = new TanksCommon.SharedObjects.GameMove() { MessageId = 8 }; var messageStream = TanksCommon.MessageEncoder.EncodeMessage(stream, theObject); messageStream.Seek(0, System.IO.SeekOrigin.Begin); var messageBytes = messageStream.ToArray(); var hash = new byte[32];//send all 0s (incorrect hash) System.Collections.Generic.List <byte> byteList = new System.Collections.Generic.List <byte>(hash); byteList.AddRange(messageBytes); _serverMessenger.SendDataToClient(byteList.ToArray()); } Thread.Sleep(200); TheMessenger.ReceivedDataLog -= theHandler; }
public void TestMessages() { using (var stream = new System.IO.MemoryStream()) { var messageStream = new System.IO.MemoryStream(); TanksCommon.SharedObjects.GameMove theObject = new TanksCommon.SharedObjects.GameMove(); theObject.GameId = 2; theObject.GunType = 5; theObject.LocationY = 3; theObject.LocationX = 4; theObject.MoveId = 7; theObject.PlayerId = 15; theObject.ShotAngle = 95; theObject.ShotPower = 86; messageStream = TanksCommon.MessageEncoder.EncodeMessage(stream, theObject); messageStream.Seek(0, System.IO.SeekOrigin.Begin); var decodedObject = TanksCommon.MessageDecoder.DecodeMessage <TanksCommon.SharedObjects.GameMove>(messageStream); Assert.AreEqual(theObject.GameId, decodedObject.GameId); Assert.AreEqual(theObject.GunType, theObject.GunType); Assert.AreEqual(theObject.LocationY, theObject.LocationY); Assert.AreEqual(theObject.LocationX, theObject.LocationX); Assert.AreEqual(theObject.MoveId, theObject.MoveId); Assert.AreEqual(theObject.PlayerId, theObject.PlayerId); Assert.AreEqual(theObject.ShotAngle, theObject.ShotAngle); Assert.AreEqual(theObject.ShotPower, theObject.ShotPower); Assert.AreEqual(theObject, theObject); Assert.AreEqual(theObject, theObject); } }
public void EncryptDecryptTest() { byte[] messageBytes = null; using (var stream = new System.IO.MemoryStream()) { var theObject = new TanksCommon.SharedObjects.GameMove() { MessageId = 8 }; var messageStream = TanksCommon.MessageEncoder.EncodeMessage(stream, theObject); messageStream.Seek(0, System.IO.SeekOrigin.Begin); messageBytes = messageStream.ToArray(); } //can only encrypt messages, only has public key TanksCommon.Encryption.EncryptioinKeys clientKey = new TanksCommon.Encryption.EncryptioinKeys(); //can decrypt messages, has public and private key TanksCommon.Encryption.EncryptioinKeys serverKey = new TanksCommon.Encryption.EncryptioinKeys(); //server provides public RSA keys and client imports them clientKey.ImportPublicKey(serverKey.ExportPublicKey()); //client can encrypt AES keys with the provided RSA keys var clientEncryptor = new GameCom.Encrypt(clientKey); //server sets public AES keys serverKey.SetIvAndSessionKey(clientKey.Iv, clientKey.SessionKey); var e1 = clientEncryptor.EncryptBytes(messageBytes); var d1 = GameCom.Encrypt.DecryptBytes(serverKey, e1); Assert.IsFalse(messageBytes.SequenceEqual(e1)); Assert.IsTrue(messageBytes.SequenceEqual(d1)); //serverKey.SetIvAndSessionKey(clientKey.Iv, clientKey.SessionKey); /*var serverEncrypter = new GameCom.Encrypt(serverKey); * * var e2 = serverEncrypter.EncryptBytes(messageBytes); * * var d2 = GameCom.Encrypt.DecryptBytes(clientKey, e2); * * Assert.IsTrue(d2.SequenceEqual(messageBytes));*/ }
public void Hash() { using (var stream = new System.IO.MemoryStream()) { var theObject = new TanksCommon.SharedObjects.GameMove() { MessageId = 8 }; var messageStream = TanksCommon.MessageEncoder.EncodeMessage(stream, theObject); messageStream.Seek(0, System.IO.SeekOrigin.Begin); var messageBytes = messageStream.ToArray(); var hash = GameCom.Hash.HashData(messageBytes); Assert.IsNotNull(hash); Assert.IsTrue(hash.Length == 32); } }
public bool SendMove(TanksCommon.SharedObjects.GameMove move) { _log.Debug($"Sending Move: {move}"); _gameServerMessenger.SendObjectToTcpClient(move); return(false); }