File: QualityEstimation.cs

package info (click to toggle)
keepass2 2.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,496 kB
  • sloc: cs: 87,098; xml: 6,569; cpp: 311; makefile: 49; sh: 9
file content (145 lines) | stat: -rw-r--r-- 4,951 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
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2012 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;

using KeePassLib.Utility;

namespace KeePassLib.Cryptography
{
	/// <summary>
	/// A class that offers static functions to estimate the quality of
	/// passwords.
	/// </summary>
	public static class QualityEstimation
	{
		private enum CharSpaceBits : uint
		{
			Control = 32,
			Alpha = 26,
			Number = 10,
			Special = 33,
			High = 112
		}

		/// <summary>
		/// Estimate the quality of a password.
		/// </summary>
		/// <param name="vPasswordChars">Password to check.</param>
		/// <returns>Estimated bit-strength of the password.</returns>
		/// <exception cref="System.ArgumentNullException">Thrown if the input
		/// parameter is <c>null</c>.</exception>
		public static uint EstimatePasswordBits(char[] vPasswordChars)
		{
			Debug.Assert(vPasswordChars != null);
			if(vPasswordChars == null) throw new ArgumentNullException("vPasswordChars");

			bool bChLower = false, bChUpper = false, bChNumber = false;
			bool bChSpecial = false, bChHigh = false, bChControl = false;
			Dictionary<char, uint> vCharCounts = new Dictionary<char, uint>();
			Dictionary<int, uint> vDifferences = new Dictionary<int, uint>();
			double dblEffectiveLength = 0.0;

			for(int i = 0; i < vPasswordChars.Length; ++i) // Get character types
			{
				char tch = vPasswordChars[i];

				if(tch < ' ') bChControl = true;
				else if((tch >= 'A') && (tch <= 'Z')) bChUpper = true;
				else if((tch >= 'a') && (tch <= 'z')) bChLower = true;
				else if((tch >= '0') && (tch <= '9')) bChNumber = true;
				else if((tch >= ' ') && (tch <= '/')) bChSpecial = true;
				else if((tch >= ':') && (tch <= '@')) bChSpecial = true;
				else if((tch >= '[') && (tch <= '`')) bChSpecial = true;
				else if((tch >= '{') && (tch <= '~')) bChSpecial = true;
				else if(tch > '~') bChHigh = true;

				double dblDiffFactor = 1.0;
				if(i >= 1)
				{
					int iDiff = (int)tch - (int)vPasswordChars[i - 1];

					uint uDiffCount;
					if(vDifferences.TryGetValue(iDiff, out uDiffCount))
					{
						++uDiffCount;
						vDifferences[iDiff] = uDiffCount;
						dblDiffFactor /= (double)uDiffCount;
					}
					else vDifferences.Add(iDiff, 1);
				}

				uint uCharCount;
				if(vCharCounts.TryGetValue(tch, out uCharCount))
				{
					++uCharCount;
					vCharCounts[tch] = uCharCount;
					dblEffectiveLength += dblDiffFactor * (1.0 / (double)uCharCount);
				}
				else
				{
					vCharCounts.Add(tch, 1);
					dblEffectiveLength += dblDiffFactor;
				}
			}

			uint uCharSpace = 0;
			if(bChControl) uCharSpace += (uint)CharSpaceBits.Control;
			if(bChUpper) uCharSpace += (uint)CharSpaceBits.Alpha;
			if(bChLower) uCharSpace += (uint)CharSpaceBits.Alpha;
			if(bChNumber) uCharSpace += (uint)CharSpaceBits.Number;
			if(bChSpecial) uCharSpace += (uint)CharSpaceBits.Special;
			if(bChHigh) uCharSpace += (uint)CharSpaceBits.High;

			if(uCharSpace == 0) return 0;

			double dblBitsPerChar = Math.Log((double)uCharSpace) / Math.Log(2.0);
			double dblRating = dblBitsPerChar * dblEffectiveLength;

#if !KeePassLibSD
			char[] vLowerCopy = new char[vPasswordChars.Length];
			for(int ilc = 0; ilc < vLowerCopy.Length; ++ilc)
				vLowerCopy[ilc] = char.ToLower(vPasswordChars[ilc]);
			if(PopularPasswords.IsPopularPassword(vLowerCopy)) dblRating /= 8.0;
			Array.Clear(vLowerCopy, 0, vLowerCopy.Length);
#endif

			return (uint)Math.Ceiling(dblRating);
		}

		/// <summary>
		/// Estimate the quality of a password.
		/// </summary>
		/// <param name="pbUnprotectedUtf8">Password to check, UTF-8 encoded.</param>
		/// <returns>Estimated bit-strength of the password.</returns>
		public static uint EstimatePasswordBits(byte[] pbUnprotectedUtf8)
		{
			if(pbUnprotectedUtf8 == null) { Debug.Assert(false); return 0; }

			char[] vChars = StrUtil.Utf8.GetChars(pbUnprotectedUtf8);
			uint uResult = EstimatePasswordBits(vChars);
			Array.Clear(vChars, 0, vChars.Length);

			return uResult;
		}
	}
}