diff --git a/ClosedXML/Excel/XLSheetProtection.cs b/ClosedXML/Excel/XLSheetProtection.cs index 9fd5108..9166280 100644 --- a/ClosedXML/Excel/XLSheetProtection.cs +++ b/ClosedXML/Excel/XLSheetProtection.cs @@ -16,7 +16,7 @@ { set { - PasswordHash = StringExtensions.GetPasswordHash(value); + PasswordHash = GetPasswordHash(value); } } @@ -66,7 +66,7 @@ else { Protected = true; - PasswordHash = StringExtensions.GetPasswordHash(password); + PasswordHash = GetPasswordHash(password); } return this; } @@ -80,7 +80,7 @@ { if (Protected) { - String hash = StringExtensions.GetPasswordHash(password); + String hash = GetPasswordHash(password); if (hash != PasswordHash) throw new ArgumentException("Invalid password"); else @@ -92,5 +92,21 @@ return this; } + + private String GetPasswordHash(String password) + { + Int32 pLength = password.Length; + Int32 hash = 0; + if (pLength == 0) return String.Empty; + + for (Int32 i = pLength - 1; i >= 0; i--) + { + hash ^= password[i]; + hash = hash >> 14 & 0x01 | hash << 1 & 0x7fff; + } + hash ^= 0x8000 | 'N' << 8 | 'K'; + hash ^= pLength; + return hash.ToString("X"); + } } } diff --git a/ClosedXML/Excel/XLWorkbook.cs b/ClosedXML/Excel/XLWorkbook.cs index 173ff20..ed6398f 100644 --- a/ClosedXML/Excel/XLWorkbook.cs +++ b/ClosedXML/Excel/XLWorkbook.cs @@ -870,7 +870,7 @@ { if (workbookPassword != null) { - var hashPassword = StringExtensions.GetPasswordHash(workbookPassword); + var hashPassword = GetPasswordHash(workbookPassword); if (LockPassword != null) { if (LockPassword != hashPassword) @@ -939,5 +939,21 @@ { Protect(false, false, workbookPassword); } + + private String GetPasswordHash(String password) + { + Int32 pLength = password.Length; + Int32 hash = 0; + if (pLength == 0) return String.Empty; + + for (Int32 i = pLength - 1; i >= 0; i--) + { + hash ^= password[i]; + hash = hash >> 14 & 0x01 | hash << 1 & 0x7fff; + } + hash ^= 0x8000 | 'N' << 8 | 'K'; + hash ^= pLength; + return hash.ToString("X"); + } } } \ No newline at end of file diff --git a/ClosedXML/Extensions.cs b/ClosedXML/Extensions.cs index f9b9a10..7655168 100644 --- a/ClosedXML/Extensions.cs +++ b/ClosedXML/Extensions.cs @@ -132,22 +132,6 @@ return value.Substring(0, 1).ToUpper() + value.Substring(1); } - - public static String GetPasswordHash(String password) - { - Int32 pLength = password.Length; - Int32 hash = 0; - if (pLength == 0) return String.Empty; - - for (Int32 i = pLength - 1; i >= 0; i--) - { - hash ^= password[i]; - hash = hash >> 14 & 0x01 | hash << 1 & 0x7fff; - } - hash ^= 0x8000 | 'N' << 8 | 'K'; - hash ^= pLength; - return hash.ToString("X"); - } } public static class DateTimeExtensions