private static void TestEncryptWithHmacHelper(int length, AxCryptOptions options) { byte[] output; byte[] hmacKey; using (MemoryStream inputStream = new MemoryStream()) { byte[] text = Resolve.RandomGenerator.Generate(length); inputStream.Write(text, 0, text.Length); inputStream.Position = 0; using (MemoryStream outputStream = new MemoryStream()) { using (V2AxCryptDocument document = new V2AxCryptDocument(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("Secret")), 100)) { document.EncryptTo(inputStream, outputStream, options); output = outputStream.ToArray(); hmacKey = document.DocumentHeaders.GetHmacKey(); } } } byte[] hmacBytesFromHeaders = new byte[V2Hmac.RequiredLength]; Array.Copy(output, output.Length - V2Hmac.RequiredLength, hmacBytesFromHeaders, 0, V2Hmac.RequiredLength); V2Hmac hmacFromHeaders = new V2Hmac(hmacBytesFromHeaders); byte[] dataToHmac = new byte[output.Length - (V2Hmac.RequiredLength + 5)]; Array.Copy(output, 0, dataToHmac, 0, dataToHmac.Length); HMACSHA512 hmac = new HMACSHA512(hmacKey); hmac.TransformFinalBlock(dataToHmac, 0, dataToHmac.Length); V2Hmac hmacFromCalculation = new V2Hmac(hmac.Hash); Assert.That(hmacFromHeaders, Is.EqualTo(hmacFromCalculation)); }
public void TestWriteWithHmac() { V2DocumentHeaders headers = new V2DocumentHeaders(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("v2passzz")), 20); byte[] output; V2HmacCalculator hmacCalculator = new V2HmacCalculator(new SymmetricKey(headers.GetHmacKey())); using (V2HmacStream <MemoryStream> hmacStream = V2HmacStream.Create <MemoryStream>(hmacCalculator, new MemoryStream())) { headers.WriteStartWithHmac(hmacStream); headers.WriteEndWithHmac(hmacCalculator, hmacStream, 0, 0); hmacStream.Flush(); output = hmacStream.Chained.ToArray(); } byte[] hmacBytesFromHeaders = new byte[V2Hmac.RequiredLength]; Array.Copy(output, output.Length - V2Hmac.RequiredLength, hmacBytesFromHeaders, 0, V2Hmac.RequiredLength); V2Hmac hmacFromHeaders = new V2Hmac(hmacBytesFromHeaders); byte[] dataToHmac = new byte[output.Length - (V2Hmac.RequiredLength + 5)]; Array.Copy(output, 0, dataToHmac, 0, dataToHmac.Length); HMACSHA512 hmac = new HMACSHA512(headers.GetHmacKey()); hmac.TransformFinalBlock(dataToHmac, 0, dataToHmac.Length); V2Hmac hmacFromCalculation = new V2Hmac(hmac.Hash); Assert.That(hmacFromHeaders, Is.EqualTo(hmacFromCalculation)); }