示例#1
0
        /// <summary>
        /// Writes protectes the workbook with a password.
        /// EPPlus uses SHA-512 as hash algorithm with a spin count of 100000.
        /// </summary>
        /// <param name="userName">The name of the person enforcing the write protection</param>
        /// <param name="password">The password. Setting the password to null or empty will remove the read-only mode.</param>
        public void SetReadOnly(string userName, string password)
        {
            UserName = userName;
            if (string.IsNullOrEmpty(password?.Trim()))
            {
                RemovePasswordAttributes();
                return;
            }
            HashAlgorithm = eHashAlgorithm.SHA512;

            var s   = new byte[16];
            var rnd = RandomNumberGenerator.Create();

            rnd.GetBytes(s);
            SaltValue = s;
            SpinCount = 100000;

            HashValue = EncryptedPackageHandler.GetPasswordHashSpinAppending(SHA512.Create(), SaltValue, password, SpinCount, 64);
        }