diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index 6df9e8e..b558cb2 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -2174,7 +2174,18 @@ if (sp == null) return; if (sp.Sheet != null) ws.Protection.Protected = sp.Sheet.Value; - if (sp.Password != null) ws.Protection.PasswordHash = sp.Password.Value; + + var algorithmName = sp.AlgorithmName?.Value ?? string.Empty; + if (String.IsNullOrEmpty(algorithmName)) + { + ws.Protection.PasswordHash = sp.Password?.Value ?? string.Empty; + } + else + { + var hashValue = sp.HashValue?.Value ?? string.Empty; + var saltValue = sp.SaltValue?.Value ?? string.Empty; + // Continue for now. + } if (sp.FormatCells != null) ws.Protection.FormatCells = !sp.FormatCells.Value; if (sp.FormatColumns != null) ws.Protection.FormatColumns = !sp.FormatColumns.Value; if (sp.FormatRows != null) ws.Protection.FormatRows = !sp.FormatRows.Value; diff --git a/ClosedXML_Tests/Excel/Worksheets/XLSheetProtectionTests.cs b/ClosedXML_Tests/Excel/Worksheets/XLSheetProtectionTests.cs new file mode 100644 index 0000000..b74eaff --- /dev/null +++ b/ClosedXML_Tests/Excel/Worksheets/XLSheetProtectionTests.cs @@ -0,0 +1,40 @@ +using ClosedXML.Excel; +using NUnit.Framework; +using System; + +namespace ClosedXML_Tests.Excel.Worksheets +{ + [TestFixture] + public class XLSheetProtectionTests + { + [Test] + public void TestUnprotectWorksheetWithNoPassword() + { + using (var stream = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Misc\SHA512PasswordProtection.xlsx"))) + using (var wb = new XLWorkbook(stream)) + { + var ws = wb.Worksheet("Sheet1"); + Assert.IsTrue(ws.Protection.Protected); + ws.Unprotect(); + Assert.IsFalse(ws.Protection.Protected); + } + } + + [Test] + public void TestWorksheetWithSHA512Protection() + { + using (var stream = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Misc\SHA512PasswordProtection.xlsx"))) + using (var wb = new XLWorkbook(stream)) + { + var ws = wb.Worksheet("Sheet2"); + Assert.IsTrue(ws.Protection.Protected); + // Protected with SHA-512 password - not yet supported. + Assert.Throws(() => ws.Unprotect()); + + // Protected with SHA-512 password - not yet supported. + Assert.Throws(() => ws.Unprotect("abc")); + Assert.IsTrue(ws.Protection.Protected); + } + } + } +} diff --git a/ClosedXML_Tests/Resource/Misc/SHA512PasswordProtection.xlsx b/ClosedXML_Tests/Resource/Misc/SHA512PasswordProtection.xlsx new file mode 100644 index 0000000..61bf9c3 --- /dev/null +++ b/ClosedXML_Tests/Resource/Misc/SHA512PasswordProtection.xlsx Binary files differ