-
Notifications
You must be signed in to change notification settings - Fork 10
/
PasswordSolver.cs
123 lines (97 loc) · 4.19 KB
/
PasswordSolver.cs
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
using BlazorAI.Shared.Types;
using GeneticSharp;
using System;
using System.Linq;
namespace BlazorAI.Shared.Solvers
{
public record PasswordSolution(string Password);
/// <summary>
/// Solver for theoretical password system which (for some reason!)
/// provides feedback on how close a password is and allows unlimited guesses.
/// Chromsome is simply a string which has it's genes (chars) crossed over
/// and mutated within the fixed character range.
/// </summary>
public class PasswordSolver : Solver<PasswordSolution>
{
// Allow characters from Space to Tilde which includes
// numbers, upper & lower case letters & punctuation
public const int CharLowerBound = 32; // ' '
public const int CharUpperBound = 127; // '~'
public PasswordSolver(string password)
{
PasswordLength = password.Length;
FitnessProvider = new PasswordFitness(password);
}
int PasswordLength { get; }
PasswordFitness FitnessProvider { get; }
protected override GeneticAlgorithm GetGA(SolverParameters parameters)
{
var adamChromosome = new PasswordChromosome(PasswordLength);
var population =
new Population(parameters.Population, parameters.Population, adamChromosome);
return new GeneticAlgorithm(
population,
FitnessProvider,
new EliteSelection(),
new UniformCrossover(),
new StringMutation());
}
protected override PasswordSolution GetSolution(IChromosome chromosome) =>
new PasswordSolution(Password: FitnessProvider.GetSolution(chromosome));
}
public class PasswordFitness : IFitness
{
public PasswordFitness(string password) => Password = password;
string Password { get; }
public string GetSolution(IChromosome chromosome) =>
new string(chromosome.GetGenes().Select(x => (char)x.Value).ToArray());
public double Evaluate(IChromosome chromosome)
{
var diff =
chromosome.GetGenes()
.Zip(Password, (x, y) => Math.Abs((char)x.Value - y))
.Sum(x => x == 0 ? 0 : x + 10); // Reward exact match
return Math.Max(0, 1.0 - (diff / (Password.Length * 50.0)));
}
}
/// <summary>
/// Char array to represent our guess
/// This seems to perform way better than when using FloatingPointChromosome
/// </summary>
public class PasswordChromosome : ChromosomeBase
{
public PasswordChromosome(int passwordLength) : base(passwordLength)
{
PasswordLength = passwordLength;
CreateGenes();
}
public int PasswordLength { get; set; }
public override Gene GenerateGene(int geneIndex)
{
int index = RandomizationProvider.Current.GetInt(
PasswordSolver.CharLowerBound, PasswordSolver.CharUpperBound);
return new Gene((char)index);
}
public override IChromosome CreateNew() => new PasswordChromosome(PasswordLength);
}
public class StringMutation : MutationBase
{
// Select random char and increase/decrease it's (ASCII) value by a random amount
// while ensuring it stays within the valid range of char values
protected override void PerformMutate(IChromosome chromosome, float probability)
{
if (RandomizationProvider.Current.GetDouble() <= probability)
{
// ~10% of range from lower to upper seems to work well
const int MaxMutationAmount = 10;
var index = RandomizationProvider.Current.GetInt(0, chromosome.Length);
int currVal = (char)chromosome.GetGene(index).Value;
var newGene = currVal +
RandomizationProvider.Current.GetInt(-MaxMutationAmount, MaxMutationAmount + 1);
newGene = Math.Min(newGene, PasswordSolver.CharUpperBound);
newGene = Math.Max(newGene, PasswordSolver.CharLowerBound);
chromosome.ReplaceGene(index, new Gene((char)newGene));
}
}
}
}