public void TestBadArguments() { V1DocumentHeaders documentHeaders = new V1DocumentHeaders(Passphrase.Empty, 37); Assert.Throws <ArgumentNullException>(() => { documentHeaders.WriteWithHmac(null); }); Assert.Throws <ArgumentNullException>(() => { documentHeaders.WriteWithoutHmac(null); }); Assert.Throws <ArgumentNullException>(() => { documentHeaders.Headers.Hmac = null; }); }
/// <summary> /// Write a copy of the current encrypted stream. Used to change meta-data /// and encryption key(s) etc. /// </summary> /// <param name="outputStream"></param> public void CopyEncryptedTo(V1DocumentHeaders outputDocumentHeaders, Stream cipherStream) { if (outputDocumentHeaders == null) { throw new ArgumentNullException("outputDocumentHeaders"); } if (cipherStream == null) { throw new ArgumentNullException("cipherStream"); } if (!cipherStream.CanSeek) { throw new ArgumentException("The output stream must support seek in order to back-track and write the HMAC."); } if (!PassphraseIsValid) { throw new InternalErrorException("Passphrase is not valid."); } using (V1HmacStream hmacStreamOutput = new V1HmacStream(outputDocumentHeaders.HmacSubkey.Key, cipherStream)) { outputDocumentHeaders.WriteWithHmac(hmacStreamOutput); using (V1AxCryptDataStream encryptedDataStream = CreateEncryptedDataStream(_reader.InputStream, DocumentHeaders.CipherTextLength)) { encryptedDataStream.CopyTo(hmacStreamOutput); if (Hmac != DocumentHeaders.Headers.Hmac) { throw new Axantum.AxCrypt.Core.Runtime.IncorrectDataException("HMAC validation error in the input stream.", ErrorStatus.HmacValidationError); } } outputDocumentHeaders.Headers.Hmac = hmacStreamOutput.HmacResult; // Rewind and rewrite the headers, now with the updated HMAC outputDocumentHeaders.WriteWithoutHmac(cipherStream); cipherStream.Position = cipherStream.Length; } }