553 lines
17 KiB
C#
553 lines
17 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Media;
|
||
using BS.Shared;
|
||
using BS.Shared.Extensions;
|
||
|
||
namespace BeWo.PasswortSecurity
|
||
{
|
||
internal class PasswortSicherheit
|
||
{
|
||
#region Konstanten und Variablen
|
||
private const string Numbers = "0123456789";
|
||
private const string LowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
|
||
private const string UppercaseLetters = "ABDEFGHIJKLMNOPQRSTUVWXYZ";
|
||
private const string LowercaseUmlauts = "äöüß";
|
||
private const string UppercaseUmlauts = "ÄÖÜ";
|
||
private const string SpecialCharacters = "\\\"\'µ@€!&/()=?+#*,.-;:_><|§$%'{[]}´`^°~²³";
|
||
|
||
|
||
private const string numbers = "0123456789";
|
||
private const string uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||
private const string lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
|
||
|
||
private const string uppercaseUmlauts = "ÄÖÜ";
|
||
private const string lowercaseUmlauts = "äöüß";
|
||
|
||
private const string specialCharacters1 = "\\\"\'µ@€!";
|
||
private const string specialCharacters2 = "&/()=?+#*";
|
||
private const string specialCharacters3 = ",.-;:_><|";
|
||
private const string specialCharacters4 = "§$%'{[]}´";
|
||
private const string specialCharacters5 = "`^°~²³";
|
||
|
||
private string partialPassword = "";
|
||
private bool isPartial;
|
||
private int passwordLengthDifference;
|
||
|
||
int score;
|
||
int previousValue;
|
||
bool schalter;
|
||
List<int> letztewerte = new List<int>();
|
||
|
||
#endregion
|
||
/*
|
||
* Länge prüfen und welche Zeichen vorhanden sind
|
||
*/
|
||
#region Passwort Stärke Ermitteln und Anzeigen
|
||
|
||
//public static Label ShowPasswordStrengthLabel(string passwordToValidate, Label passwordStrengthLabel)
|
||
//{
|
||
// var passwordStrength = CalculatePasswordStrength(passwordToValidate);
|
||
|
||
// var visibility = Visibility.Visible;
|
||
// var content = "Passwortstärke: ";
|
||
// var foreground = new SolidColorBrush(Colors.Black);
|
||
|
||
// switch(passwordStrength)
|
||
// {
|
||
// case 0:
|
||
// content += "sehr schwach";
|
||
// foreground = new SolidColorBrush(Colors.Red);
|
||
// break;
|
||
// case 1:
|
||
// content += "schwach";
|
||
// foreground = new SolidColorBrush(Colors.OrangeRed);
|
||
// break;
|
||
// case 2:
|
||
// content += "mittel";
|
||
// foreground = new SolidColorBrush(Colors.Yellow);
|
||
// break;
|
||
// case 3:
|
||
// content += "stark";
|
||
// foreground = new SolidColorBrush(Colors.Green);
|
||
// break;
|
||
// case 4:
|
||
// content += "sehr stark";
|
||
// foreground = new SolidColorBrush(Colors.Green);
|
||
// break;
|
||
// default:
|
||
// visibility = Visibility.Hidden;
|
||
// content = string.Empty;
|
||
// break;
|
||
// }
|
||
|
||
// passwordStrengthLabel.Visibility = visibility;
|
||
// passwordStrengthLabel.Foreground = foreground;
|
||
// passwordStrengthLabel.Content = content;
|
||
|
||
// return passwordStrengthLabel;
|
||
//}
|
||
|
||
//private static int CalculatePasswordStrength(string passwordToValidate)
|
||
//{
|
||
// var length = passwordToValidate?.Length ?? 0;
|
||
// if(string.IsNullOrEmpty(passwordToValidate) || length < 6)
|
||
// {
|
||
// return -1;
|
||
// }
|
||
|
||
// var score = 0;
|
||
|
||
// if(length < 9)
|
||
// {
|
||
// score += 1;
|
||
// }
|
||
// else if(length < 12)
|
||
// {
|
||
// score += 2;
|
||
// }
|
||
// else if(length < 18)
|
||
// {
|
||
// score += 3;
|
||
// }
|
||
// else
|
||
// {
|
||
// score += 4;
|
||
// }
|
||
|
||
// var numberCount = passwordToValidate.Count(c => Numbers.Contains(c));
|
||
// var lowercaseLetterCount = passwordToValidate.Count(c => LowercaseLetters.Contains(c));
|
||
// var uppercaseLetterCount = passwordToValidate.Count(c => UppercaseLetters.Contains(c));
|
||
// var lowercaseUmlautsCount = passwordToValidate.Count(c => LowercaseUmlauts.Contains(c));
|
||
// var uppercaseUmlautsCount = passwordToValidate.Count(c => UppercaseUmlauts.Contains(c));
|
||
// var specialCharacterCount = passwordToValidate.Count(c => SpecialCharacters.Contains(c));
|
||
|
||
// score += numberCount * 3;
|
||
// score += lowercaseLetterCount * 2;
|
||
// score += uppercaseLetterCount * 3;
|
||
// score += lowercaseUmlautsCount * 2;
|
||
// score += uppercaseUmlautsCount * 2;
|
||
// score += specialCharacterCount * 4;
|
||
|
||
// if(score < 16)
|
||
// {
|
||
// return 0;
|
||
// }
|
||
|
||
// if(score < 56)
|
||
// {
|
||
// return 1;
|
||
// }
|
||
|
||
// if(score < 116)
|
||
// {
|
||
// return 2;
|
||
// }
|
||
|
||
// return score < 156 ? 3 : 4;
|
||
//}
|
||
|
||
// Score, PreivousValue, Schalter, und LetzteWerte werden im Objekt gepeichert.
|
||
public Label ErmittleUndZeigePasswortStaerke(string passwordToValidate, Label passwordLabel)
|
||
{
|
||
if (!passwordToValidate.IsNullOrEmpty())
|
||
{
|
||
var sx = "";
|
||
var passwordStrength = -1;
|
||
|
||
// Es wird durch jedes Zeichen des Passworts einzeln gegangen
|
||
foreach (var token in passwordToValidate)
|
||
{
|
||
sx += token.ToString();
|
||
|
||
if (sx.Length < partialPassword.Length)
|
||
{
|
||
isPartial = true;
|
||
passwordLengthDifference = partialPassword.Length - sx.Length;
|
||
}
|
||
|
||
if (!partialPassword.Equals(sx))
|
||
{
|
||
partialPassword = sx;
|
||
}
|
||
|
||
passwordStrength = PasswortStaerkeErmittler(sx);
|
||
}
|
||
|
||
return ZeigePwStaerke(passwordStrength, passwordLabel);
|
||
}
|
||
|
||
score = 0;
|
||
previousValue = 0;
|
||
|
||
return ZeigePwStaerke(-1, passwordLabel);
|
||
}
|
||
|
||
private int PasswortStaerkeErmittler(string passwordToValidate)
|
||
{
|
||
var passwordStrength = 0;
|
||
previousValue = 0;
|
||
|
||
// Es handelt sich nur um einen Teil des zu überprüfenden Passworts
|
||
if (isPartial)
|
||
{
|
||
// Der zu überprüfende Teil ist nur ein Zeichen
|
||
if (passwordLengthDifference == 1)
|
||
{
|
||
score -= letztewerte.Last();
|
||
letztewerte.RemoveAt(letztewerte.Count - 1);
|
||
}
|
||
// Der zu überprüfende Teil ist mehr als ein Zeichen
|
||
else
|
||
{
|
||
for (int s = 0; s < passwordLengthDifference; s++)
|
||
{
|
||
score -= letztewerte.Last();
|
||
letztewerte.RemoveAt(letztewerte.Count - 1);
|
||
}
|
||
}
|
||
|
||
isPartial = false;
|
||
}
|
||
else
|
||
{
|
||
//Passwortlängen Bonuspunkte
|
||
if (passwordToValidate.Length >= 6 && passwordToValidate.Length < 9)
|
||
{
|
||
score += 1;
|
||
previousValue = 1;
|
||
schalter = true;
|
||
}
|
||
|
||
if (passwordToValidate.Length >= 9 && passwordToValidate.Length < 12)
|
||
{
|
||
score += 2;
|
||
previousValue = 2;
|
||
schalter = true;
|
||
}
|
||
|
||
if (passwordToValidate.Length >= 12 && passwordToValidate.Length < 18)
|
||
{
|
||
score += 3;
|
||
previousValue = 3;
|
||
schalter = true;
|
||
}
|
||
|
||
if (passwordToValidate.Length >= 18)
|
||
{
|
||
score += 4;
|
||
previousValue = 4;
|
||
schalter = true;
|
||
}
|
||
|
||
//Zahlen
|
||
if (passwordToValidate.Any(x => numbers.Contains(x)))
|
||
{
|
||
score += 3;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 3;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 3;
|
||
}
|
||
}
|
||
|
||
// Großbuchstaben
|
||
if (passwordToValidate.Any(x => uppercaseLetters.Contains(x)))
|
||
{
|
||
score += 3;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 3;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 3;
|
||
}
|
||
}
|
||
|
||
// Großumlaute
|
||
if (passwordToValidate.Any(x => uppercaseUmlauts.Contains(x)))
|
||
{
|
||
score += 2;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 2;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 2;
|
||
}
|
||
}
|
||
|
||
if (passwordToValidate.Any(x => lowercaseLetters.Contains(x)))
|
||
{
|
||
score += 2;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 2;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 2;
|
||
}
|
||
}
|
||
|
||
// Kleinumlaute
|
||
if (passwordToValidate.Any(x => lowercaseUmlauts.Contains(x)))
|
||
{
|
||
score += 2;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 2;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 2;
|
||
}
|
||
}
|
||
|
||
// Sonderzeichen
|
||
if (passwordToValidate.Any(x => specialCharacters1.Contains(x)))
|
||
{
|
||
score += 4;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 4;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 4;
|
||
}
|
||
}
|
||
|
||
// Sonderzeichen 2
|
||
if (passwordToValidate.Any(x => specialCharacters2.Contains(x)))
|
||
{
|
||
score += 4;
|
||
|
||
if (schalter)
|
||
previousValue += 4;
|
||
else
|
||
previousValue = 4;
|
||
}
|
||
|
||
// Sonderzeichen 3
|
||
if(passwordToValidate.Any(x => specialCharacters3.Contains(x)))
|
||
{
|
||
score += 4;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 4;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 4;
|
||
}
|
||
}
|
||
|
||
// Sonderzeichen 4
|
||
if(passwordToValidate.Any(x => specialCharacters4.Contains(x)))
|
||
{
|
||
score += 4;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 4;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 4;
|
||
}
|
||
}
|
||
|
||
// Sonderzeichen 5
|
||
if(passwordToValidate.Any(x => specialCharacters5.Contains(x)))
|
||
{
|
||
score += 4;
|
||
|
||
if (schalter)
|
||
{
|
||
previousValue += 4;
|
||
}
|
||
else
|
||
{
|
||
previousValue = 4;
|
||
}
|
||
}
|
||
|
||
letztewerte.Add(previousValue);
|
||
}
|
||
|
||
//Sehr Schwach
|
||
if (score <= 15)
|
||
{
|
||
passwordStrength = 0;
|
||
}
|
||
|
||
//Schwach
|
||
if (score >= 16 && score <= 55)
|
||
{
|
||
passwordStrength = 1;
|
||
}
|
||
|
||
//Mittelmäßig
|
||
if (score >= 56 && score <= 115)
|
||
{
|
||
passwordStrength = 2;
|
||
}
|
||
|
||
//Stark
|
||
if (score >= 116 && score <= 155)
|
||
{
|
||
passwordStrength = 3;
|
||
}
|
||
|
||
//Sehr Stark
|
||
if (score >= 156)
|
||
{
|
||
passwordStrength = 4;
|
||
}
|
||
|
||
return passwordStrength;
|
||
}
|
||
|
||
private Label ZeigePwStaerke(int passwordStrength, Label passwortprüfer)
|
||
{
|
||
switch (passwordStrength)
|
||
{
|
||
case -1:
|
||
passwortprüfer.Visibility = Visibility.Hidden;
|
||
passwortprüfer.Content = "";
|
||
|
||
break;
|
||
case 0:
|
||
passwortprüfer.Visibility = Visibility.Visible;
|
||
passwortprüfer.Foreground = new SolidColorBrush(Colors.Red);
|
||
passwortprüfer.Content = "Passwortstärke: Sehr schwach";
|
||
|
||
break;
|
||
|
||
case 1:
|
||
passwortprüfer.Visibility = Visibility.Visible;
|
||
passwortprüfer.Foreground = new SolidColorBrush(Colors.OrangeRed);
|
||
passwortprüfer.Content = "Passwortstärke: Schwach";
|
||
|
||
break;
|
||
|
||
case 2:
|
||
passwortprüfer.Visibility = Visibility.Visible;
|
||
passwortprüfer.Foreground = new SolidColorBrush(Colors.Yellow);
|
||
passwortprüfer.Content = "Passwortstärke: Mittel";
|
||
|
||
break;
|
||
|
||
case 3:
|
||
passwortprüfer.Visibility = Visibility.Visible;
|
||
passwortprüfer.Foreground = new SolidColorBrush(Colors.Green);
|
||
passwortprüfer.Content = "Passwortstärke: Stark";
|
||
|
||
break;
|
||
|
||
case 4:
|
||
passwortprüfer.Visibility = Visibility.Visible;
|
||
passwortprüfer.Foreground = new SolidColorBrush(Colors.Green);
|
||
passwortprüfer.Content = "Passwortstärke: Sehr stark";
|
||
|
||
break;
|
||
}
|
||
|
||
return passwortprüfer;
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region PasswortStärke des Angemeldeten User Passworts
|
||
public PasswortSecurityStrength PasswortSecurityStaerke(string s)
|
||
{
|
||
if (!s.IsNullOrEmpty())
|
||
{
|
||
string sx = "";
|
||
|
||
int i = -1;
|
||
|
||
foreach (var item in s)
|
||
{
|
||
sx += item.ToString();
|
||
|
||
if (sx.Length < partialPassword.Length)
|
||
{
|
||
isPartial = true;
|
||
passwordLengthDifference = partialPassword.Length - sx.Length;
|
||
}
|
||
|
||
if (!partialPassword.Equals(sx))
|
||
partialPassword = sx;
|
||
|
||
i = PasswortStaerkeErmittler(sx);
|
||
}
|
||
|
||
return ZeigeSecutityStaerke(i);
|
||
}
|
||
else
|
||
{
|
||
score = 0;
|
||
previousValue = 0;
|
||
|
||
return ZeigeSecutityStaerke(-1);
|
||
}
|
||
}
|
||
|
||
private PasswortSecurityStrength ZeigeSecutityStaerke(int i)
|
||
{
|
||
PasswortSecurityStrength pwsSec = PasswortSecurityStrength.SehrSchwach;
|
||
|
||
switch (i)
|
||
{
|
||
case -1:
|
||
pwsSec = PasswortSecurityStrength.SehrSchwach;
|
||
break;
|
||
case 0:
|
||
pwsSec = PasswortSecurityStrength.Schwach;
|
||
|
||
break;
|
||
|
||
case 1:
|
||
pwsSec = PasswortSecurityStrength.Schwach;
|
||
|
||
break;
|
||
|
||
case 2:
|
||
pwsSec = PasswortSecurityStrength.Mittelmass;
|
||
|
||
break;
|
||
|
||
case 3:
|
||
pwsSec = PasswortSecurityStrength.Stark;
|
||
|
||
break;
|
||
|
||
case 4:
|
||
pwsSec = PasswortSecurityStrength.SehrStark;
|
||
|
||
break;
|
||
}
|
||
|
||
return pwsSec;
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
}
|