diff --git a/ClosedXML/Excel/XLSheetProtection.cs b/ClosedXML/Excel/XLSheetProtection.cs index 9166280..9fd5108 100644 --- a/ClosedXML/Excel/XLSheetProtection.cs +++ b/ClosedXML/Excel/XLSheetProtection.cs @@ -16,7 +16,7 @@ { set { - PasswordHash = GetPasswordHash(value); + PasswordHash = StringExtensions.GetPasswordHash(value); } } @@ -66,7 +66,7 @@ else { Protected = true; - PasswordHash = GetPasswordHash(password); + PasswordHash = StringExtensions.GetPasswordHash(password); } return this; } @@ -80,7 +80,7 @@ { if (Protected) { - String hash = GetPasswordHash(password); + String hash = StringExtensions.GetPasswordHash(password); if (hash != PasswordHash) throw new ArgumentException("Invalid password"); else @@ -92,21 +92,5 @@ 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 ed6398f..173ff20 100644 --- a/ClosedXML/Excel/XLWorkbook.cs +++ b/ClosedXML/Excel/XLWorkbook.cs @@ -870,7 +870,7 @@ { if (workbookPassword != null) { - var hashPassword = GetPasswordHash(workbookPassword); + var hashPassword = StringExtensions.GetPasswordHash(workbookPassword); if (LockPassword != null) { if (LockPassword != hashPassword) @@ -939,21 +939,5 @@ { 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 7655168..f9b9a10 100644 --- a/ClosedXML/Extensions.cs +++ b/ClosedXML/Extensions.cs @@ -132,6 +132,22 @@ 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