[9580] | 1 | using OfficeOpenXml.Utils;
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Globalization;
|
---|
| 5 | using System.Linq;
|
---|
| 6 | using System.Security.Cryptography;
|
---|
| 7 | using System.Text;
|
---|
| 8 | using System.Xml;
|
---|
| 9 |
|
---|
| 10 | namespace OfficeOpenXml
|
---|
| 11 | {
|
---|
| 12 | /// <summary>
|
---|
| 13 | /// Algorithm for password hash
|
---|
| 14 | /// </summary>
|
---|
| 15 | internal enum eProtectedRangeAlgorithm
|
---|
| 16 | {
|
---|
| 17 | /// <summary>
|
---|
| 18 | /// Specifies that the MD2 algorithm, as defined by RFC 1319, shall be used.
|
---|
| 19 | /// </summary>
|
---|
| 20 | MD2,
|
---|
| 21 | /// <summary>
|
---|
| 22 | /// Specifies that the MD4 algorithm, as defined by RFC 1319, shall be used.
|
---|
| 23 | /// </summary>
|
---|
| 24 | MD4,
|
---|
| 25 | /// <summary>
|
---|
| 26 | /// Specifies that the MD5 algorithm, as defined by RFC 1319, shall be used.
|
---|
| 27 | /// </summary>
|
---|
| 28 | MD5,
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// Specifies that the RIPEMD-128 algorithm, as defined by RFC 1319, shall be used.
|
---|
| 31 | /// </summary>
|
---|
| 32 | RIPEMD128,
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// Specifies that the RIPEMD-160 algorithm, as defined by ISO/IEC10118-3:2004 shall be used.
|
---|
| 35 | /// </summary>
|
---|
| 36 | RIPEMD160,
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// Specifies that the SHA-1 algorithm, as defined by ISO/IEC 10118-3:2004 shall be used.
|
---|
| 39 | /// </summary>
|
---|
| 40 | SHA1,
|
---|
| 41 | /// <summary>
|
---|
| 42 | /// Specifies that the SHA-256 algorithm, as defined by ISO/IEC10118-3:2004 shall be used.
|
---|
| 43 | /// </summary>
|
---|
| 44 | SHA256,
|
---|
| 45 | /// <summary>
|
---|
| 46 | /// Specifies that the SHA-384 algorithm, as defined by ISO/IEC 10118-3:2004 shall be used.
|
---|
| 47 | /// </summary>
|
---|
| 48 | SHA384,
|
---|
| 49 | /// <summary>
|
---|
| 50 | /// Specifies that the SHA-512 algorithm, as defined by ISO/IEC10118-3:2004 shall be used.
|
---|
| 51 | /// </summary>
|
---|
| 52 | SHA512,
|
---|
| 53 | /// <summary>
|
---|
| 54 | /// Specifies that the WHIRLPOOL algorithm, as defined by ISO/IEC 10118-3:2004 shall be used.
|
---|
| 55 | /// </summary>
|
---|
| 56 | WHIRLPOOL
|
---|
| 57 | }
|
---|
| 58 | public class ExcelProtectedRange : XmlHelper
|
---|
| 59 | {
|
---|
| 60 | public string Name
|
---|
| 61 | {
|
---|
| 62 | get
|
---|
| 63 | {
|
---|
| 64 | return GetXmlNodeString("@name");
|
---|
| 65 | }
|
---|
| 66 | set
|
---|
| 67 | {
|
---|
| 68 | SetXmlNodeString("@name",value);
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | ExcelAddress _address=null;
|
---|
| 72 | public ExcelAddress Address
|
---|
| 73 | {
|
---|
| 74 | get
|
---|
| 75 | {
|
---|
| 76 | if(_address==null)
|
---|
| 77 | {
|
---|
| 78 | _address=new ExcelAddress(GetXmlNodeString("@sqref"));
|
---|
| 79 | }
|
---|
| 80 | return _address;
|
---|
| 81 | }
|
---|
| 82 | set
|
---|
| 83 | {
|
---|
| 84 | SetXmlNodeString("@sqref", SqRefUtility.ToSqRefAddress(value.Address));
|
---|
| 85 | _address=value;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | internal ExcelProtectedRange(string name, ExcelAddress address, XmlNamespaceManager ns, XmlNode topNode) :
|
---|
| 90 | base(ns,topNode)
|
---|
| 91 | {
|
---|
| 92 | Name = name;
|
---|
| 93 | Address = address;
|
---|
| 94 | }
|
---|
| 95 | /// <summary>
|
---|
| 96 | /// Sets the password for the range
|
---|
| 97 | /// </summary>
|
---|
| 98 | /// <param name="password"></param>
|
---|
| 99 | public void SetPassword(string password)
|
---|
| 100 | {
|
---|
| 101 | var byPwd = Encoding.Unicode.GetBytes(password);
|
---|
| 102 | var rnd = RandomNumberGenerator.Create();
|
---|
| 103 | var bySalt=new byte[16];
|
---|
| 104 | rnd.GetBytes(bySalt);
|
---|
| 105 |
|
---|
| 106 | //Default SHA512 and 10000 spins
|
---|
| 107 | Algorithm=eProtectedRangeAlgorithm.SHA512;
|
---|
| 108 | SpinCount = SpinCount < 100000 ? 100000 : SpinCount;
|
---|
| 109 |
|
---|
| 110 | //Combine salt and password and calculate the initial hash
|
---|
| 111 | var hp=new SHA512CryptoServiceProvider();
|
---|
| 112 | var buffer=new byte[byPwd.Length + bySalt.Length];
|
---|
| 113 | Array.Copy(bySalt, buffer, bySalt.Length);
|
---|
| 114 | Array.Copy(byPwd, 0, buffer, 16, byPwd.Length);
|
---|
| 115 | var hash = hp.ComputeHash(buffer);
|
---|
| 116 |
|
---|
| 117 | //Now iterate the number of spinns.
|
---|
| 118 | for (var i = 0; i < SpinCount; i++)
|
---|
| 119 | {
|
---|
| 120 | buffer=new byte[hash.Length+4];
|
---|
| 121 | Array.Copy(hash, buffer, hash.Length);
|
---|
| 122 | Array.Copy(BitConverter.GetBytes(i), 0, buffer, hash.Length, 4);
|
---|
| 123 | hash = hp.ComputeHash(buffer);
|
---|
| 124 | }
|
---|
| 125 | Salt = Convert.ToBase64String(bySalt);
|
---|
| 126 | Hash = Convert.ToBase64String(hash);
|
---|
| 127 | }
|
---|
| 128 | public string SecurityDescriptor
|
---|
| 129 | {
|
---|
| 130 | get
|
---|
| 131 | {
|
---|
| 132 | return GetXmlNodeString("@securityDescriptor");
|
---|
| 133 | }
|
---|
| 134 | set
|
---|
| 135 | {
|
---|
| 136 | SetXmlNodeString("@securityDescriptor",value);
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | internal int SpinCount
|
---|
| 140 | {
|
---|
| 141 | get
|
---|
| 142 | {
|
---|
| 143 | return GetXmlNodeInt("@spinCount");
|
---|
| 144 | }
|
---|
| 145 | set
|
---|
| 146 | {
|
---|
| 147 | SetXmlNodeString("@spinCount",value.ToString(CultureInfo.InvariantCulture));
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | internal string Salt
|
---|
| 151 | {
|
---|
| 152 | get
|
---|
| 153 | {
|
---|
| 154 | return GetXmlNodeString("@saltValue");
|
---|
| 155 | }
|
---|
| 156 | set
|
---|
| 157 | {
|
---|
| 158 | SetXmlNodeString("@saltValue", value);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | internal string Hash
|
---|
| 162 | {
|
---|
| 163 | get
|
---|
| 164 | {
|
---|
| 165 | return GetXmlNodeString("@hashValue");
|
---|
| 166 | }
|
---|
| 167 | set
|
---|
| 168 | {
|
---|
| 169 | SetXmlNodeString("@hashValue", value);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | eProtectedRangeAlgorithm _algorithm = eProtectedRangeAlgorithm.SHA512;
|
---|
| 173 | internal eProtectedRangeAlgorithm Algorithm
|
---|
| 174 | {
|
---|
| 175 | get
|
---|
| 176 | {
|
---|
| 177 | var v=GetXmlNodeString("@algorithmName");
|
---|
| 178 | return (eProtectedRangeAlgorithm)Enum.Parse(typeof(eProtectedRangeAlgorithm), v.Replace("-", ""));
|
---|
| 179 | }
|
---|
| 180 | set
|
---|
| 181 | {
|
---|
| 182 | var v = value.ToString();
|
---|
| 183 | if(v.StartsWith("SHA"))
|
---|
| 184 | {
|
---|
| 185 | v=v.Insert(3,"-");
|
---|
| 186 | }
|
---|
| 187 | else if(v.StartsWith("RIPEMD"))
|
---|
| 188 | {
|
---|
| 189 | v=v.Insert(6,"-");
|
---|
| 190 | }
|
---|
| 191 | SetXmlNodeString("@algorithmName", v);
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|