Skip to content

Commit

Permalink
PR #509: Make PutNextEntry throw if the entry is AES and no password …
Browse files Browse the repository at this point in the history
…has been set

refs #507
  • Loading branch information
Numpsy authored Oct 17, 2020
1 parent 91050e6 commit 9e02750
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ public void PutNextEntry(ZipEntry entry)
throw new NotImplementedException("Compression method not supported");
}

// A password must have been set in order to add AES encrypted entries
if (entry.AESKeySize > 0 && string.IsNullOrEmpty(this.Password))
{
throw new InvalidOperationException("The Password property must be set before AES encrypted entries can be added");
}

int compressionLevel = defaultCompressionLevel;

// Clear flags that the library manages internally
Expand Down
18 changes: 18 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,5 +502,23 @@ public void ShouldBeAbleToReadEntriesWithInvalidFileNames()
}
}
}

/// <summary>
/// Test for https://github.com/icsharpcode/SharpZipLib/issues/507
/// </summary>
[Test]
[Category("Zip")]
public void AddingAnAESEntryWithNoPasswordShouldThrow()
{
using (var memoryStream = new MemoryStream())
{
using (var outStream = new ZipOutputStream(memoryStream))
{
var newEntry = new ZipEntry("test") { AESKeySize = 256 };

Assert.Throws<InvalidOperationException>(() => outStream.PutNextEntry(newEntry));
}
}
}
}
}

0 comments on commit 9e02750

Please sign in to comment.