File: PwCharSet.cs

package info (click to toggle)
keepass2 2.47%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,992 kB
  • sloc: cs: 111,053; xml: 5,956; cpp: 308; makefile: 48; sh: 48
file content (306 lines) | stat: -rw-r--r-- 9,708 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace KeePassLib.Cryptography.PasswordGenerator
{
	public sealed class PwCharSet
	{
		public static readonly string UpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		public static readonly string LowerCase = "abcdefghijklmnopqrstuvwxyz";
		public static readonly string Digits = "0123456789";

		public static readonly string UpperConsonants = "BCDFGHJKLMNPQRSTVWXYZ";
		public static readonly string LowerConsonants = "bcdfghjklmnpqrstvwxyz";
		public static readonly string UpperVowels = "AEIOU";
		public static readonly string LowerVowels = "aeiou";

		public static readonly string Punctuation = @",.;:";
		public static readonly string Brackets = @"[]{}()<>";

		public static readonly string Special = "!\"#$%&'*+,./:;=?@\\^`|~";
		public static readonly string PrintableAsciiSpecial = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";

		public static readonly string UpperHex = "0123456789ABCDEF";
		public static readonly string LowerHex = "0123456789abcdef";

		public static readonly string LookAlike = @"O0l1I|";

		/// <summary>
		/// Latin-1 Supplement except U+00A0 (NBSP) and U+00AD (SHY).
		/// </summary>
		public static readonly string Latin1S =
			"\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7" +
			"\u00A8\u00A9\u00AA\u00AB\u00AC\u00AE\u00AF" +
			"\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7" +
			"\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF" +
			"\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7" +
			"\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF" +
			"\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7" +
			"\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF" +
			"\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7" +
			"\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF" +
			"\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7" +
			"\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF";

		internal static readonly string MenuAccels = PwCharSet.LowerCase + PwCharSet.Digits;

		[Obsolete]
		public static string SpecialChars { get { return PwCharSet.Special; } }
		[Obsolete]
		public static string HighAnsiChars { get { return PwCharSet.Latin1S; } }

		private const int CharTabSize = 0x10000 / 8;

		private List<char> m_lChars = new List<char>();
		private byte[] m_vTab = new byte[CharTabSize];

		/// <summary>
		/// Create a new, empty character set.
		/// </summary>
		public PwCharSet()
		{
			Debug.Assert(PwCharSet.Latin1S.Length == (16 * 6 - 2));
		}

		public PwCharSet(string strCharSet)
		{
			Add(strCharSet);
		}

		/// <summary>
		/// Number of characters in this set.
		/// </summary>
		public uint Size
		{
			get { return (uint)m_lChars.Count; }
		}

		/// <summary>
		/// Get a character of the set using an index.
		/// </summary>
		/// <param name="uPos">Index of the character to get.</param>
		/// <returns>Character at the specified position. If the index is invalid,
		/// an <c>ArgumentOutOfRangeException</c> is thrown.</returns>
		public char this[uint uPos]
		{
			get
			{
				if(uPos >= (uint)m_lChars.Count)
					throw new ArgumentOutOfRangeException("uPos");

				return m_lChars[(int)uPos];
			}
		}

		/// <summary>
		/// Remove all characters from this set.
		/// </summary>
		public void Clear()
		{
			m_lChars.Clear();
			Array.Clear(m_vTab, 0, m_vTab.Length);
		}

		public bool Contains(char ch)
		{
			return (((m_vTab[ch / 8] >> (ch % 8)) & 1) != char.MinValue);
		}

		public bool Contains(string strCharacters)
		{
			Debug.Assert(strCharacters != null);
			if(strCharacters == null) throw new ArgumentNullException("strCharacters");

			foreach(char ch in strCharacters)
			{
				if(!Contains(ch)) return false;
			}

			return true;
		}

		/// <summary>
		/// Add characters to the set.
		/// </summary>
		/// <param name="ch">Character to add.</param>
		public void Add(char ch)
		{
			if(ch == char.MinValue) { Debug.Assert(false); return; }

			if(!Contains(ch))
			{
				m_lChars.Add(ch);
				m_vTab[ch / 8] |= (byte)(1 << (ch % 8));
			}
		}

		/// <summary>
		/// Add characters to the set.
		/// </summary>
		/// <param name="strCharSet">String containing characters to add.</param>
		public void Add(string strCharSet)
		{
			Debug.Assert(strCharSet != null);
			if(strCharSet == null) throw new ArgumentNullException("strCharSet");

			foreach(char ch in strCharSet)
				Add(ch);
		}

		public void Add(string strCharSet1, string strCharSet2)
		{
			Add(strCharSet1);
			Add(strCharSet2);
		}

		public void Add(string strCharSet1, string strCharSet2, string strCharSet3)
		{
			Add(strCharSet1);
			Add(strCharSet2);
			Add(strCharSet3);
		}

		public void AddRange(char chMin, char chMax)
		{
			for(char ch = chMin; ch < chMax; ++ch)
				Add(ch);

			Add(chMax);
		}

		public bool AddCharSet(char chCharSetIdentifier)
		{
			bool bResult = true;

			switch(chCharSetIdentifier)
			{
				case 'a': Add(PwCharSet.LowerCase, PwCharSet.Digits); break;
				case 'A': Add(PwCharSet.LowerCase, PwCharSet.UpperCase,
					PwCharSet.Digits); break;
				case 'U': Add(PwCharSet.UpperCase, PwCharSet.Digits); break;
				case 'c': Add(PwCharSet.LowerConsonants); break;
				case 'C': Add(PwCharSet.LowerConsonants,
					PwCharSet.UpperConsonants); break;
				case 'z': Add(PwCharSet.UpperConsonants); break;
				case 'd': Add(PwCharSet.Digits); break; // Digit
				case 'h': Add(PwCharSet.LowerHex); break;
				case 'H': Add(PwCharSet.UpperHex); break;
				case 'l': Add(PwCharSet.LowerCase); break;
				case 'L': Add(PwCharSet.LowerCase, PwCharSet.UpperCase); break;
				case 'u': Add(PwCharSet.UpperCase); break;
				case 'p': Add(PwCharSet.Punctuation); break;
				case 'b': Add(PwCharSet.Brackets); break;
				case 's': Add(PwCharSet.PrintableAsciiSpecial); break;
				case 'S': Add(PwCharSet.UpperCase, PwCharSet.LowerCase);
					Add(PwCharSet.Digits, PwCharSet.PrintableAsciiSpecial); break;
				case 'v': Add(PwCharSet.LowerVowels); break;
				case 'V': Add(PwCharSet.LowerVowels, PwCharSet.UpperVowels); break;
				case 'Z': Add(PwCharSet.UpperVowels); break;
				case 'x': Add(PwCharSet.Latin1S); break;
				default: bResult = false; break;
			}

			return bResult;
		}

		public bool Remove(char ch)
		{
			m_vTab[ch / 8] &= (byte)(~(1 << (ch % 8)));
			return m_lChars.Remove(ch);
		}

		public bool Remove(string strCharacters)
		{
			Debug.Assert(strCharacters != null);
			if(strCharacters == null) throw new ArgumentNullException("strCharacters");

			bool bResult = true;
			foreach(char ch in strCharacters)
			{
				if(!Remove(ch)) bResult = false;
			}

			return bResult;
		}

		public bool RemoveIfAllExist(string strCharacters)
		{
			Debug.Assert(strCharacters != null);
			if(strCharacters == null) throw new ArgumentNullException("strCharacters");

			if(!Contains(strCharacters))
				return false;

			return Remove(strCharacters);
		}

		/// <summary>
		/// Convert the character set to a string containing all its characters.
		/// </summary>
		/// <returns>String containing all character set characters.</returns>
		public override string ToString()
		{
			StringBuilder sb = new StringBuilder(m_lChars.Count);
			foreach(char ch in m_lChars)
				sb.Append(ch);

			return sb.ToString();
		}

		public string PackAndRemoveCharRanges()
		{
			StringBuilder sb = new StringBuilder();

			sb.Append(RemoveIfAllExist(PwCharSet.UpperCase) ? 'U' : '_');
			sb.Append(RemoveIfAllExist(PwCharSet.LowerCase) ? 'L' : '_');
			sb.Append(RemoveIfAllExist(PwCharSet.Digits) ? 'D' : '_');
			sb.Append(RemoveIfAllExist(PwCharSet.Special) ? 'S' : '_');
			sb.Append(RemoveIfAllExist(PwCharSet.Punctuation) ? 'P' : '_');
			sb.Append(RemoveIfAllExist("-") ? 'm' : '_');
			sb.Append(RemoveIfAllExist("_") ? 'u' : '_');
			sb.Append(RemoveIfAllExist(" ") ? 's' : '_');
			sb.Append(RemoveIfAllExist(PwCharSet.Brackets) ? 'B' : '_');
			sb.Append(RemoveIfAllExist(PwCharSet.Latin1S) ? 'H' : '_');

			return sb.ToString();
		}

		public void UnpackCharRanges(string strRanges)
		{
			if(strRanges == null) { Debug.Assert(false); return; }
			if(strRanges.Length < 10) { Debug.Assert(false); return; }

			if(strRanges[0] != '_') Add(PwCharSet.UpperCase);
			if(strRanges[1] != '_') Add(PwCharSet.LowerCase);
			if(strRanges[2] != '_') Add(PwCharSet.Digits);
			if(strRanges[3] != '_') Add(PwCharSet.Special);
			if(strRanges[4] != '_') Add(PwCharSet.Punctuation);
			if(strRanges[5] != '_') Add('-');
			if(strRanges[6] != '_') Add('_');
			if(strRanges[7] != '_') Add(' ');
			if(strRanges[8] != '_') Add(PwCharSet.Brackets);
			if(strRanges[9] != '_') Add(PwCharSet.Latin1S);
		}
	}
}