public static byte[] MakeResponse(string coreUrl, long userId, long sessionID, string diffKey, string result) { long salt = OTP.GenerateRandomID(); long time = OTP.GetTime(); long length = result.Length; byte[] saltArray = BitConverter.GetBytes(salt); byte[] timeArray = BitConverter.GetBytes(time); byte[] lengthArray = BitConverter.GetBytes(length); byte[] sessionIDArray = BitConverter.GetBytes(sessionID); byte[] DiffKey = Convert.FromBase64String(diffKey); byte[] clearMsgKey = new byte [saltArray.Length + sessionIDArray.Length + timeArray.Length + lengthArray.Length ]; Buffer.BlockCopy(saltArray, 0, clearMsgKey, 0, saltArray.Length); Buffer.BlockCopy(sessionIDArray, 0, clearMsgKey, saltArray.Length, sessionIDArray.Length); Buffer.BlockCopy(timeArray, 0, clearMsgKey, saltArray.Length + sessionIDArray.Length, timeArray.Length); Buffer.BlockCopy(lengthArray, 0, clearMsgKey, saltArray.Length + sessionIDArray.Length + timeArray.Length, lengthArray.Length); byte[] preRuntimeAESKey = new byte[DiffKey.Length + sessionIDArray.Length]; Buffer.BlockCopy(DiffKey, 0, preRuntimeAESKey, 0, DiffKey.Length); Buffer.BlockCopy(sessionIDArray, 0, preRuntimeAESKey, DiffKey.Length, sessionIDArray.Length); byte[] runtimeAESKey = SHA256.Create().ComputeHash(preRuntimeAESKey); using (var ms = new MemoryStream()) using (var writer = new BinaryWriter(ms)) { var msg = new AuthMessage(writer); msg.addRow(clearMsgKey); msg.addRow(coreUrl); msg.addRow(result); return(UserAuth.GenerateMessage(new AES(runtimeAESKey).Encrypt(ms.ToArray()))); } }
public static byte[] GenerateMessage(params object[] rows) { using (var memory = new MemoryStream()) using (var writer = new BinaryWriter(memory)) { var msg = new AuthMessage(writer); foreach (var item in rows) { msg.addRow(item); } return(memory.ToArray()); } }