1 | /*******************************************************************************
|
---|
2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
3 | *
|
---|
4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2011 Jan Källman
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | * See the GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
21 | *
|
---|
22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
24 | *
|
---|
25 | * Code change notes:
|
---|
26 | *
|
---|
27 | * Author Change Date
|
---|
28 | *******************************************************************************
|
---|
29 | * Jan Källman Added 12-APR-2012
|
---|
30 | *******************************************************************************/
|
---|
31 | using System;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using System.Linq;
|
---|
34 | using System.Text;
|
---|
35 | using System.Security.Cryptography;
|
---|
36 |
|
---|
37 | namespace OfficeOpenXml.VBA
|
---|
38 | {
|
---|
39 | /// <summary>
|
---|
40 | /// Vba security properties
|
---|
41 | /// </summary>
|
---|
42 | public class ExcelVbaProtection
|
---|
43 | {
|
---|
44 | ExcelVbaProject _project;
|
---|
45 | internal ExcelVbaProtection(ExcelVbaProject project)
|
---|
46 | {
|
---|
47 | _project = project;
|
---|
48 | VisibilityState = true;
|
---|
49 | }
|
---|
50 | /// <summary>
|
---|
51 | /// Specifies whether access to the VBA project was restricted by the user
|
---|
52 | /// </summary>
|
---|
53 | public bool UserProtected { get; internal set; }
|
---|
54 | /// <summary>
|
---|
55 | /// Specifies whether access to the VBA project was restricted by the VBA host application
|
---|
56 | /// </summary>
|
---|
57 | public bool HostProtected { get; internal set; }
|
---|
58 | /// <summary>
|
---|
59 | /// Specifies whether access to the VBA project was restricted by the VBA project editor
|
---|
60 | /// </summary>
|
---|
61 | public bool VbeProtected { get; internal set; }
|
---|
62 | /// <summary>
|
---|
63 | /// Specifies whether the VBA project is visible.
|
---|
64 | /// </summary>
|
---|
65 | public bool VisibilityState { get; internal set; }
|
---|
66 | internal byte[] PasswordHash { get; set; }
|
---|
67 | internal byte[] PasswordKey { get; set; }
|
---|
68 | /// <summary>
|
---|
69 | /// Password protect the VBA project.
|
---|
70 | /// An empty string or null will remove the password protection
|
---|
71 | /// </summary>
|
---|
72 | /// <param name="Password">The password</param>
|
---|
73 | public void SetPassword(string Password)
|
---|
74 | {
|
---|
75 |
|
---|
76 | if (string.IsNullOrEmpty(Password))
|
---|
77 | {
|
---|
78 | PasswordHash = null;
|
---|
79 | PasswordKey = null;
|
---|
80 | VbeProtected = false;
|
---|
81 | HostProtected = false;
|
---|
82 | UserProtected = false;
|
---|
83 | VisibilityState = true;
|
---|
84 | _project.ProjectID = "{5DD90D76-4904-47A2-AF0D-D69B4673604E}";
|
---|
85 | }
|
---|
86 | else
|
---|
87 | {
|
---|
88 | //Join Password and Key
|
---|
89 | byte[] data;
|
---|
90 | //Set the key
|
---|
91 | PasswordKey = new byte[4];
|
---|
92 | RandomNumberGenerator r = RandomNumberGenerator.Create();
|
---|
93 | r.GetBytes(PasswordKey);
|
---|
94 |
|
---|
95 | data = new byte[Password.Length + 4];
|
---|
96 | Array.Copy(Encoding.GetEncoding(_project.CodePage).GetBytes(Password), data, Password.Length);
|
---|
97 | VbeProtected = true;
|
---|
98 | VisibilityState = false;
|
---|
99 | Array.Copy(PasswordKey, 0, data, data.Length - 4, 4);
|
---|
100 |
|
---|
101 | //Calculate Hash
|
---|
102 | var provider = SHA1.Create();
|
---|
103 | PasswordHash = provider.ComputeHash(data);
|
---|
104 | _project.ProjectID = "{00000000-0000-0000-0000-000000000000}";
|
---|
105 | }
|
---|
106 | }
|
---|
107 | //public void ValidatePassword(string Password)
|
---|
108 | //{
|
---|
109 |
|
---|
110 | //}
|
---|
111 | }
|
---|
112 | }
|
---|