public void TestWriteLong() { byte[] data = new byte[4096 * 8]; fixed (byte* lp = data) { using (var bs = new BinaryStreamPointerWrapper(lp, data.Length)) { DebugStopwatch sw = new DebugStopwatch(); var time = sw.TimeEventMedian(() => { for (int repeat = 0; repeat < 1000; repeat++) { bs.Position = 0; for (int x = 0; x < 1000; x++) { bs.Write((long)x); bs.Write((long)x); bs.Write((long)x); bs.Write((long)x); } } }); Console.WriteLine((4 * 1000 * 1000) / time / 1000 / 1000); } } }
static unsafe byte[] CreateSessionData(byte[] sessionSecret, SrpUserCredential user) { byte[] initializationVector = SaltGenerator.Create(16); int len = sessionSecret.Length; int blockLen = (len + 15) & ~15; //Add 15, then round down. (Effecitvely rounds up to the nearest 128 bit boundary). byte[] dataToEncrypt = new byte[blockLen]; sessionSecret.CopyTo(dataToEncrypt, 0); //fill the remainder of the block with random bits if (len != blockLen) { SaltGenerator.Create(blockLen - len).CopyTo(dataToEncrypt, len); } byte[] ticket = new byte[1 + 16 + 8 + 16 + 2 + blockLen + 32]; var aes = new RijndaelManaged(); aes.Key = user.ServerEncryptionkey; aes.IV = initializationVector; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.None; var decrypt = aes.CreateEncryptor(); var encryptedData = decrypt.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length); fixed (byte* lp = ticket) { var stream = new BinaryStreamPointerWrapper(lp, ticket.Length); stream.Write((byte)1); stream.Write((short)len); stream.Write(user.ServerKeyName); stream.Write(DateTime.UtcNow); stream.Write(initializationVector); stream.Write(encryptedData); stream.Write(HMAC<Sha256Digest>.Compute(user.ServerHMACKey, ticket, 0, ticket.Length - 32)); } return ticket; }