NanoXLSX.Reader 3.0.0-rc.2
Loading...
Searching...
No Matches
LegacyPasswordReader.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Xml;
10using NanoXLSX.Exceptions;
13using NanoXLSX.Registry;
14using NanoXLSX.Registry.Attributes;
16
18{
22 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.PasswordReader)]
23 public class LegacyPasswordReader : IPasswordReader
24 {
25 private string passwordHash;
26
30 public virtual bool ContemporaryAlgorithmDetected { get; private set; }
31
35 public virtual PasswordType Type { get; private set; }
36
40 public virtual ReaderOptions Options { get; private set; }
41
45 public virtual string PasswordHash
46 {
47 get { return passwordHash; }
48 set { passwordHash = value; }
49 }
50
51
55 internal LegacyPasswordReader()
56 {
57 }
58
64 public virtual void Init(PasswordType type, ReaderOptions readerOptions)
65 {
66 this.Type = type;
67 this.Options = readerOptions;
68 }
69
74 public virtual void ReadXmlAttributes(XmlNode node)
75 {
76 string attribute;
77 if (Type == PasswordType.WorkbookProtection)
78 {
79 attribute = ReaderUtils.GetAttribute(node, "workbookAlgorithmName");
80 }
81 else
82 {
83 attribute = ReaderUtils.GetAttribute(node, "algorithmName");
84 }
85 if (attribute != null)
86 {
87 if (Options.IgnoreNotSupportedPasswordAlgorithms)
88 {
89 this.ContemporaryAlgorithmDetected = true;
90 }
91 else
92 {
93 throw new NotSupportedContentException("A not supported, contemporary password algorithm for the worksheet protection was detected. Check possible packages to add support to NanoXLSX, or ignore this error by a reader option");
94 }
95 }
96
97 if (Type == PasswordType.WorkbookProtection)
98 {
99 attribute = ReaderUtils.GetAttribute(node, "workbookPassword");
100 if (attribute != null)
101 {
102 this.PasswordHash = attribute;
103 }
104 }
105 else
106 {
107 attribute = ReaderUtils.GetAttribute(node, "password");
108 if (attribute != null)
109 {
110 this.PasswordHash = attribute;
111 }
112 }
113 }
114
119 public virtual string GetPassword()
120 {
121 return null; // The reader cannot recover the plain text password
122 }
123
128 public virtual bool PasswordIsSet()
129 {
130 return passwordHash != null || ContemporaryAlgorithmDetected;
131 }
132
138 public virtual void CopyFrom(IPassword passwordInstance)
139 {
140 throw new NotImplementedException();
141 }
142
148 public virtual void SetPassword(string plainText)
149 {
150 throw new NotImplementedException();
151 }
152
157 public virtual void UnsetPassword()
158 {
159 throw new NotImplementedException();
160 }
161 }
162}
Static class that contains enums for password handling.
Static class with common util methods, used during reading XLSX files.
static string GetAttribute(XmlNode node, string targetName, string fallbackValue=null)
Gets the XML attribute of the passed XML node by its name.
Class representing a reader for legacy passwords.
virtual string GetPassword()
Gets the password. This method is not supported in a reader and will always return null.
virtual string PasswordHash
Gets or sets the password hash.
virtual void CopyFrom(IPassword passwordInstance)
Not relevant for the reader (inherited from IPassword).
virtual void SetPassword(string plainText)
Not relevant for the reader (inherited from IPassword).
virtual void Init(PasswordType type, ReaderOptions readerOptions)
Initialization method (interface implementation).
virtual bool PasswordIsSet()
Indicates whether a password is set. This can be the case, if a legacy or contemporary password was d...
virtual PasswordType Type
Current target type of the password instance.
virtual void ReadXmlAttributes(XmlNode node)
Reads the attributes of the passed XML node that contains password information.
virtual void UnsetPassword()
Not relevant for the reader (inherited from IPassword).
virtual ReaderOptions Options
Reader options.
virtual bool ContemporaryAlgorithmDetected
Gets whether a contemporary password algorithm was detected (not supported by core functionality).
The reader options define global rules, applied when loading a worksheet. The options are mainly to o...
Interface, used by password readers.