public void TestWriteLong() { byte[] data = new byte[4096 * 8]; fixed(byte *lp = data) { using (BinaryStreamPointerWrapper bs = new BinaryStreamPointerWrapper(lp, data.Length)) { DebugStopwatch sw = new DebugStopwatch(); double 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); } } }
private unsafe void CreateResumeTicket(Guid userToken, out byte[] ticket, out byte[] sessionSecret) { #if SQLCLR ticket = null; sessionSecret = null; #else State state = m_state; //Ticket Structure: // byte TicketVersion = 1 // Guid ServerKeyName // DateTime AuthenticationTime // byte[16] IV // byte[] Encrypted Session Data 32+16 // byte[32] HMAC (Sha2-256) const int encryptedLength = 32 + 16; const int ticketSize = 1 + 16 + 8 + 16 + encryptedLength + 32; byte[] initializationVector = SaltGenerator.Create(16); sessionSecret = SaltGenerator.Create(32); byte[] userTokenBytes = userToken.ToRfcBytes(); byte[] dataToEncrypt = new byte[encryptedLength]; sessionSecret.CopyTo(dataToEncrypt, 0); userTokenBytes.CopyTo(dataToEncrypt, sessionSecret.Length); ticket = new byte[ticketSize]; using (Aes aes = Cipher.CreateAes()) { aes.Key = state.ServerEncryptionkey; aes.IV = initializationVector; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.None; ICryptoTransform decrypt = aes.CreateEncryptor(); byte[] encryptedData = decrypt.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length); fixed(byte *lp = ticket) { BinaryStreamPointerWrapper stream = new BinaryStreamPointerWrapper(lp, ticket.Length); stream.Write((byte)1); stream.Write(state.ServerKeyName); stream.Write(DateTime.UtcNow.RoundDownToNearestMinute()); stream.Write(initializationVector); stream.Write(encryptedData); //Encrypted data, 32 byte session key, n byte user token stream.Write(HMAC <Sha256Digest> .Compute(state.ServerHMACKey, ticket, 0, ticket.Length - 32)); } } #endif }
private 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. (Effectively 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]; using (Aes aes = Cipher.CreateAes()) { aes.Key = user.ServerEncryptionkey; aes.IV = initializationVector; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.None; ICryptoTransform decrypt = aes.CreateEncryptor(); byte[] encryptedData = decrypt.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length); fixed(byte *lp = ticket) { using (BinaryStreamPointerWrapper 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); }