Skip to content

Commit

Permalink
Moved to using double for guess counts instead of long.
Browse files Browse the repository at this point in the history
This is a breaking change.
#23
  • Loading branch information
Tony Richards committed Oct 29, 2020
1 parent c24b0d8 commit a86ba9b
Show file tree
Hide file tree
Showing 26 changed files with 50 additions and 108 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

8 changes: 0 additions & 8 deletions TODO-Global.txt

This file was deleted.

2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 5.0.{build}
version: 6.0.{build}
image: Visual Studio 2019

init:
Expand Down
5 changes: 0 additions & 5 deletions postbuild.ps1

This file was deleted.

1 change: 0 additions & 1 deletion test.ps1

This file was deleted.

4 changes: 0 additions & 4 deletions test_dictionary.txt

This file was deleted.

13 changes: 0 additions & 13 deletions tests.bat

This file was deleted.

14 changes: 0 additions & 14 deletions zxcvbn-core-test-console/Program.cs

This file was deleted.

14 changes: 0 additions & 14 deletions zxcvbn-core-test-console/zxcvbn-core-test-console.csproj

This file was deleted.

2 changes: 1 addition & 1 deletion zxcvbn-core-test/Scoring/BruteForceScoringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void MostGuessableMatchSequenceRetursnOneMatchForEmptySequence()
result.Sequence.Should().BeEquivalentTo(expected);
}

private Match CreateTestMatch(int i, int j, long guesses)
private Match CreateTestMatch(int i, int j, double guesses)
{
return new DateMatch
{
Expand Down
4 changes: 2 additions & 2 deletions zxcvbn-core-test/Scoring/SpatialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public void AccountsForTurnPositionsDirectionsAndStartingKey()
var l = match.Token.Length;
var s = SpatialGuessesCalculator.KeyboardStartingPositions;
var d = SpatialGuessesCalculator.KeyboardAverageDegree;
long expected = 0;
var expected = 0.0;

for (var i = 2; i <= l; i++)
{
for (var j = 1; j <= Math.Min(match.Turns, i - 1); j++)
{
expected += PasswordScoring.Binomial(i - 1, j - 1) * s * (long)Math.Pow(d, j);
expected += PasswordScoring.Binomial(i - 1, j - 1) * s * Math.Pow(d, j);
}
}

Expand Down
6 changes: 3 additions & 3 deletions zxcvbn-core/Matcher/Matches/DictionaryMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DictionaryMatch : Match
/// <summary>
/// Gets the base guesses associated with the matched word.
/// </summary>
public long BaseGuesses { get; internal set; }
public double BaseGuesses { get; internal set; }

/// <summary>
/// Gets the name of the dictionary containing the matched word.
Expand All @@ -27,7 +27,7 @@ public class DictionaryMatch : Match
/// <summary>
/// Gets the number of L33T variations associated with this match.
/// </summary>
public long L33tVariations { get; internal set; }
public double L33tVariations { get; internal set; }

/// <summary>
/// Gets the dictionary word matched to.
Expand Down Expand Up @@ -60,7 +60,7 @@ public class DictionaryMatch : Match
/// <summary>
/// Gets the number of uppercase variations associated with this match.
/// </summary>
public long UppercaseVariations { get; internal set; }
public double UppercaseVariations { get; internal set; }

/// <summary>
/// Gets or sets the l33t character mappings that are in use for this match.
Expand Down
2 changes: 1 addition & 1 deletion zxcvbn-core/Matcher/Matches/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class Match
/// <summary>
/// Gets the number of guesses associated with this match.
/// </summary>
public long Guesses { get; internal set; }
public double Guesses { get; internal set; }

/// <summary>
/// Gets log10(number of guesses) associated with this match.
Expand Down
2 changes: 1 addition & 1 deletion zxcvbn-core/Matcher/Matches/RepeatMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RepeatMatch : Match
/// <summary>
/// Gets the base number guesses associated with the base matches.
/// </summary>
public long BaseGuesses { get; internal set; }
public double BaseGuesses { get; internal set; }

/// <summary>
/// Gets the base matches that are repeated.
Expand Down
2 changes: 1 addition & 1 deletion zxcvbn-core/MostGuessableMatchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class MostGuessableMatchResult
/// <summary>
/// Gets or sets the guesses the match is estimated to need.
/// </summary>
public long Guesses { get; set; }
public double Guesses { get; set; }

/// <summary>
/// Gets or sets the password being assessed.
Expand Down
8 changes: 4 additions & 4 deletions zxcvbn-core/OptimalValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public OptimalValues(int length)
{
for (var i = 0; i < length; i++)
{
G.Add(new Dictionary<int, long>());
Pi.Add(new Dictionary<int, long>());
G.Add(new Dictionary<int, double>());
Pi.Add(new Dictionary<int, double>());
M.Add(new Dictionary<int, Match>());
}
}

/// <summary>
/// Gets or sets the overall metric for the best guess.
/// </summary>
public List<Dictionary<int, long>> G { get; set; } = new List<Dictionary<int, long>>();
public List<Dictionary<int, double>> G { get; set; } = new List<Dictionary<int, double>>();

/// <summary>
/// Gets or sets the best match at a given length.
Expand All @@ -35,6 +35,6 @@ public OptimalValues(int length)
/// <summary>
/// Gets or sets the the product term of the metric for the best guess.
/// </summary>
public List<Dictionary<int, long>> Pi { get; set; } = new List<Dictionary<int, long>>();
public List<Dictionary<int, double>> Pi { get; set; } = new List<Dictionary<int, double>>();
}
}
18 changes: 9 additions & 9 deletions zxcvbn-core/PasswordScoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ internal static class PasswordScoring
/// <param name="n">n.</param>
/// <param name="k">k.</param>
/// <returns>Binomial coefficient.</returns>
public static long Binomial(int n, int k)
public static double Binomial(int n, int k)
{
if (k > n) return 0;
if (k == 0) return 1;

long r = 1;
var r = 1.0;
for (var d = 1; d <= k; ++d)
{
r *= n;
Expand All @@ -41,18 +41,18 @@ public static long Binomial(int n, int k)
/// <param name="match">The match.</param>
/// <param name="password">The actual password.</param>
/// <returns>The guesses estimate.</returns>
public static long EstimateGuesses(Match match, string password)
public static double EstimateGuesses(Match match, string password)
{
if (match.Guesses != 0)
return match.Guesses;

var minGuesses = 1;
var minGuesses = 1.0;
if (match.Token.Length < password.Length)
{
minGuesses = match.Token.Length == 1 ? BruteForceGuessesCalculator.MinSubmatchGuessesSingleCharacter : BruteForceGuessesCalculator.MinSubmatchGuessesMultiCharacter;
}

long guesses = 0;
var guesses = 0.0;

switch (match.Pattern)
{
Expand Down Expand Up @@ -127,7 +127,7 @@ public static MostGuessableMatchResult MostGuessableMatchSequence(string passwor
var optimalMatchSequence = Unwind(optimal, password.Length);
var optimalL = optimalMatchSequence.Count;

long guesses;
double guesses;

if (password.Length == 0)
guesses = 1;
Expand Down Expand Up @@ -162,11 +162,11 @@ private static void BruteforceUpdate(string password, OptimalValues optimal, int
}
}

private static long Factorial(long n)
private static double Factorial(double n)
{
if (n < 2)
return 1;
var f = 1;
var f = 1.0;

for (var i = 2; i <= n; i++)
f *= i;
Expand Down Expand Up @@ -222,7 +222,7 @@ private static void Update(string password, OptimalValues optimal, Match m, int

var g = Factorial(l) * pi;
if (!excludeAdditive)
g += (long)Math.Pow(MinimumGuessesBeforeGrowingSequence, l - 1);
g += Math.Pow(MinimumGuessesBeforeGrowingSequence, l - 1);

foreach (var competingL in optimal.G[k].Keys)
{
Expand Down
2 changes: 1 addition & 1 deletion zxcvbn-core/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Result
/// <summary>
/// Gets the number of guesses the password is estimated to need.
/// </summary>
public long Guesses { get; internal set; }
public double Guesses { get; internal set; }

/// <summary>
/// Gets log10(the number of guesses) the password is estimated to need.
Expand Down
10 changes: 5 additions & 5 deletions zxcvbn-core/Scoring/BruteForceGuessesCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ internal class BruteForceGuessesCalculator
/// <summary>
/// The minimum submatch guesses for a multi character string.
/// </summary>
public const int MinSubmatchGuessesMultiCharacter = 50;
public const double MinSubmatchGuessesMultiCharacter = 50;

/// <summary>
/// The minimum submatch guesses for a single character.
/// </summary>
public const int MinSubmatchGuessesSingleCharacter = 10;
public const double MinSubmatchGuessesSingleCharacter = 10;

private const int BruteforceCardinality = 10;
private const double BruteforceCardinality = 10;

/// <summary>
/// Estimates the attempts required to guess the password.
/// </summary>
/// <param name="match">The match.</param>
/// <returns>The guesses estimate.</returns>
public static long CalculateGuesses(BruteForceMatch match)
public static double CalculateGuesses(BruteForceMatch match)
{
var guesses = Math.Pow(BruteforceCardinality, match.Token.Length);
if (double.IsPositiveInfinity(guesses))
guesses = double.MaxValue;

var minGuesses = match.Token.Length == 1 ? MinSubmatchGuessesSingleCharacter + 1 : MinSubmatchGuessesMultiCharacter + 1;

return (long)Math.Max(guesses, minGuesses);
return Math.Max(guesses, minGuesses);
}
}
}
4 changes: 2 additions & 2 deletions zxcvbn-core/Scoring/DateGuessesCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ internal class DateGuessesCalculator
/// </summary>
/// <param name="match">The match.</param>
/// <returns>The guesses estimate.</returns>
public static long CalculateGuesses(DateMatch match)
public static double CalculateGuesses(DateMatch match)
{
var yearSpace = Math.Max(Math.Abs(match.Year - DateMatcher.ReferenceYear), MinimumYearSpace);

var guesses = yearSpace * 365;
var guesses = yearSpace * 365.0;
if (!string.IsNullOrEmpty(match.Separator))
guesses *= 4;

Expand Down
12 changes: 6 additions & 6 deletions zxcvbn-core/Scoring/DictionaryGuessesCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class DictionaryGuessesCalculator
/// </summary>
/// <param name="match">The match.</param>
/// <returns>The guesses estimate.</returns>
public static long CalculateGuesses(DictionaryMatch match)
public static double CalculateGuesses(DictionaryMatch match)
{
match.BaseGuesses = match.Rank;
match.UppercaseVariations = UppercaseVariations(match.Token);
Expand All @@ -29,12 +29,12 @@ public static long CalculateGuesses(DictionaryMatch match)
/// </summary>
/// <param name="match">The match.</param>
/// <returns>The number of possible variations.</returns>
internal static long L33tVariations(DictionaryMatch match)
internal static double L33tVariations(DictionaryMatch match)
{
if (!match.L33t)
return 1;

long variations = 1;
var variations = 1.0;

foreach (var subbed in match.Sub.Keys)
{
Expand All @@ -49,7 +49,7 @@ internal static long L33tVariations(DictionaryMatch match)
else
{
var p = Math.Min(u, s);
long possibilities = 0;
var possibilities = 0.0;
for (var i = 1; i <= p; i++)
possibilities += PasswordScoring.Binomial(u + s, i);
variations *= possibilities;
Expand All @@ -64,7 +64,7 @@ internal static long L33tVariations(DictionaryMatch match)
/// </summary>
/// <param name="token">The token.</param>
/// <returns>The number of possible variations.</returns>
internal static long UppercaseVariations(string token)
internal static double UppercaseVariations(string token)
{
if (token.All(c => char.IsLower(c)) || token.ToLower() == token)
return 1;
Expand All @@ -76,7 +76,7 @@ internal static long UppercaseVariations(string token)

var u = token.Count(c => char.IsUpper(c));
var l = token.Count(c => char.IsLower(c));
long variations = 0;
var variations = 0.0;

for (var i = 1; i <= Math.Min(u, l); i++)
variations += PasswordScoring.Binomial(u + l, i);
Expand Down
2 changes: 1 addition & 1 deletion zxcvbn-core/Scoring/RegexGuessesCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class RegexGuessesCalculator
/// </summary>
/// <param name="match">The match.</param>
/// <returns>The guesses estimate.</returns>
public static long CalculateGuesses(RegexMatch match)
public static double CalculateGuesses(RegexMatch match)
{
switch (match.RegexName)
{
Expand Down
2 changes: 1 addition & 1 deletion zxcvbn-core/Scoring/RepeatGuessesCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class RepeatGuessesCalculator
/// </summary>
/// <param name="match">The match.</param>
/// <returns>The guesses estimate.</returns>
public static long CalculateGuesses(RepeatMatch match)
public static double CalculateGuesses(RepeatMatch match)
{
return match.BaseGuesses * match.RepeatCount;
}
Expand Down
4 changes: 2 additions & 2 deletions zxcvbn-core/Scoring/SequenceGuessesCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ internal class SequenceGuessesCalculator
/// </summary>
/// <param name="match">The match.</param>
/// <returns>The guesses estimate.</returns>
public static long CalculateGuesses(SequenceMatch match)
public static double CalculateGuesses(SequenceMatch match)
{
int baseGuesses;
double baseGuesses;

if (ObviousStartCharacters.Contains(match.Token[0]))
{
Expand Down
Loading

0 comments on commit a86ba9b

Please sign in to comment.