9using System.Collections.Generic;
10using System.Runtime.InteropServices;
37 private SecureString password;
57 this.PasswordHash =
null;
66 if (password !=
null && password.Length > 0)
68 return GetPasswordOfSecureString(password);
79 if (
string.IsNullOrEmpty(plainText))
86 password = GetSecureString(plainText);
119 this.password = GetSecureString(passwordInstance.
GetPassword());
120 if (this.GetType() == passwordInstance.GetType())
134 if (
string.IsNullOrEmpty(password)) {
return string.Empty; }
135 int passwordLength = password.Length;
136 int passwordHash = 0;
138 for (
int i = passwordLength; i > 0; i--)
140 character = password[i - 1];
141 passwordHash = ((passwordHash >> 14) & 0x01) | ((passwordHash << 1) & 0x7fff);
142 passwordHash ^= character;
144 passwordHash = ((passwordHash >> 14) & 0x01) | ((passwordHash << 1) & 0x7fff);
145 passwordHash ^= (0x8000 | (
'N' << 8) |
'K');
146 passwordHash ^= passwordLength;
155 private static SecureString GetSecureString(
string plaintextPassword)
158 if (
string.IsNullOrEmpty(plaintextPassword))
160#pragma warning disable CA1825
162#pragma warning restore CA1825
166 chars = plaintextPassword.ToCharArray();
168 SecureString str =
new SecureString();
169 foreach (
char c
in chars)
181 private static string GetPasswordOfSecureString(SecureString secureString)
183 IntPtr unmanagedString = IntPtr.Zero;
186 unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
187 return Marshal.PtrToStringUni(unmanagedString);
191 Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
205 Type == password.Type &&
218 var hashCode = 1034998357;
219 hashCode = hashCode * -1521134295 +
Type.GetHashCode();
220 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(
PasswordHash);
221 hashCode = hashCode * -1521134295 +
PasswordIsSet().GetHashCode();
override int GetHashCode()
Gets the hash code of the password instance. Note that this is not the actual password hash.
string GetPassword()
Gets the pain text password.
override bool Equals(object obj)
Returns whether two instances are the same.
void UnsetPassword()
Removes the password form the current instance.
PasswordType Type
Current target type of the password instance.
void SetPassword(string plainText)
Sets the current password and calculates the hash.
LegacyPassword(PasswordType type)
Default constructor with parameter.
void CopyFrom(IPassword passwordInstance)
Copes all data from another class instance.
string PasswordHash
Gets or sets the hashed password.
static string GenerateLegacyPasswordHash(string password)
Method to generate a legacy (Excel internal) password hash, to protect workbooks or worksheets This m...
PasswordType
Target type of the password.
@ WorksheetProtection
Password is used to protect a worksheet.
@ WorkbookProtection
Password is used to protect a workbook.
bool PasswordIsSet()
Gets whether a password was set.
Class providing general comparator methods.
static bool CompareSecureStrings(SecureString value1, SecureString value2)
Compares whether the content of two SecureString instances are equal. The comparison method tries to ...
Class providing static methods to parse string values to specific types or to print object as languag...
static readonly CultureInfo InvariantCulture
Constant for number conversion. The invariant culture (represents mostly the US numbering scheme) ens...
Interface to represent a protection password, either for workbooks or worksheets. The implementations...
string PasswordHash
Gets or sets the password hash.
string GetPassword()
Gets the password as plain text.