Skip to content

Commit

Permalink
Last bits of tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Richards committed Oct 28, 2020
1 parent 5d10039 commit fa8dec9
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 41 deletions.
14 changes: 14 additions & 0 deletions zxcvbn-core-list-builder/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1200:Using directives should be placed correctly", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces should not be omitted", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "This normalization isn't security critical.")]
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1208:System using directives should be placed before other using directives", Justification = "Using CodeMaid for this task.")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "This isn't actually publicly released.")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "No header required.")]
28 changes: 17 additions & 11 deletions zxcvbn-core-list-builder/ListBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;

namespace zxcvbn_core_list_builder
namespace Zxcvbn.ListBuilder
{
internal static class ListBuilder
{
/// <summary>
/// Returns the maximum number of words for a dictionary.
/// null means take everything.
/// </summary>
private static readonly Dictionary<string, int?> _dictionaryLimits = new Dictionary<string, int?>
private static readonly ReadOnlyDictionary<string, int?> DictionaryLimits = new ReadOnlyDictionary<string, int?>(new Dictionary<string, int?>
{
{ "us_tv_and_film", 30000 },
{ "english", 30000 },
{ "passwords", 30000 },
{ "surnames", 10000 },
{ "male_names", null },
{ "female_names", null },
};
});

/// <summary>
/// Filters the frequency lists to remove:
/// - Tokens which are short and rare
/// - Tokens that are already in another dictionary and a lower rank
/// - Tokens that end up beyond the end of the limits in <see cref="_dictionaryLimits"/>
/// - Tokens that end up beyond the end of the limits in <see cref="DictionaryLimits"/>.
/// </summary>
/// <param name="frequencyLists">The list to filter.</param>
/// <returns>
/// The filtered output.
/// </returns>
public static Dictionary<string, List<string>> FilterFrequencyLists(Dictionary<string, Dictionary<string, int>> frequencyLists)
{
var filteredTokenAndRank = new Dictionary<string, Dictionary<string, int>>();
Expand All @@ -53,7 +58,7 @@ internal static class ListBuilder
minimumValues[token] = new RankDictionaryName
{
Name = name,
Rank = rank
Rank = rank,
};
}
else
Expand All @@ -63,7 +68,7 @@ internal static class ListBuilder
minimumValues[token] = new RankDictionaryName
{
Rank = rank,
Name = name
Name = name,
};
}
}
Expand All @@ -80,7 +85,7 @@ internal static class ListBuilder

if (token == "o")
{
Console.Write("");
Console.Write(string.Empty);
}

if (minimumValues[token].Name != name)
Expand All @@ -99,8 +104,8 @@ internal static class ListBuilder
var name = kvp.Key;
var values = kvp.Value;
var res = values.OrderBy(s => s.Value).Select(kvp => kvp.Key);
if (_dictionaryLimits[name].HasValue)
res = res.Take(_dictionaryLimits[name].Value);
if (DictionaryLimits[name].HasValue)
res = res.Take(DictionaryLimits[name].Value);
result[name] = res.ToList();
}

Expand All @@ -114,7 +119,7 @@ internal static class ListBuilder
foreach (var file in Directory.GetFiles(directory))
{
var name = Path.GetFileNameWithoutExtension(file);
if (!_dictionaryLimits.ContainsKey(name))
if (!DictionaryLimits.ContainsKey(name))
{
Console.WriteLine($"Warning: {name} is in directory {directory} but was not expected. Skipped.");
continue;
Expand All @@ -129,10 +134,11 @@ internal static class ListBuilder
var token = line.Split(" ")[0];
tokenToRank[token] = rank;
}

result[name] = tokenToRank;
}

foreach (var expectedKey in _dictionaryLimits.Keys)
foreach (var expectedKey in DictionaryLimits.Keys)
{
if (!result.ContainsKey(expectedKey))
{
Expand Down
2 changes: 1 addition & 1 deletion zxcvbn-core-list-builder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using System.Reflection;

namespace zxcvbn_core_list_builder
namespace Zxcvbn.ListBuilder
{
internal class Program
{
Expand Down
3 changes: 2 additions & 1 deletion zxcvbn-core-list-builder/RankDictionaryName.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace zxcvbn_core_list_builder
namespace Zxcvbn.ListBuilder
{
internal struct RankDictionaryName
{
public string Name { get; set; }

public int Rank { get; set; }
}
}
9 changes: 8 additions & 1 deletion zxcvbn-core-list-builder/zxcvbn-core-list-builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>zxcvbn_core_list_builder</RootNamespace>
<RootNamespace>Zxcvbn.ListBuilder</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions zxcvbn-core-test/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1200:Using directives should be placed correctly", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces should not be omitted", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "This normalization isn't security critical")]
[assembly: SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "This normalization isn't security critical.")]
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1208:System using directives should be placed before other using directives", Justification = "Using CodeMaid for this task.")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "This isn't actually publicly released")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "This isn't actually publicly released.")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "No header required.")]
1 change: 1 addition & 0 deletions zxcvbn-core/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1200:Using directives should be placed correctly", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces should not be omitted", Justification = "To match this project's coding style.")]
[assembly: SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "This normalization isn't security critical")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "No header required.")]
38 changes: 19 additions & 19 deletions zxcvbn-core/Matcher/L33tMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,6 @@ namespace Zxcvbn.Matcher
/// </summary>
internal class L33tMatcher : IMatcher
{
/// <summary>
/// The table of l33t transforms.
/// </summary>
internal static ReadOnlyDictionary<char, char[]> L33tTable = new ReadOnlyDictionary<char, char[]>(new Dictionary<char, char[]>
{
['a'] = new[] { '4', '@' },
['b'] = new[] { '8' },
['c'] = new[] { '(', '{', '[', '<' },
['e'] = new[] { '3' },
['g'] = new[] { '6', '9' },
['i'] = new[] { '1', '!', '|' },
['l'] = new[] { '1', '|', '7' },
['o'] = new[] { '0' },
['s'] = new[] { '$', '5' },
['t'] = new[] { '+', '7' },
['x'] = new[] { '%' },
['z'] = new[] { '2' },
});

private readonly IEnumerable<IMatcher> dictionaryMatchers;

/// <summary>
Expand All @@ -52,6 +33,25 @@ public L33tMatcher(DictionaryMatcher dictionaryMatcher)
{
}

/// <summary>
/// Gets or sets the table of l33t transforms.
/// </summary>
internal static ReadOnlyDictionary<char, char[]> L33tTable { get; set; } = new ReadOnlyDictionary<char, char[]>(new Dictionary<char, char[]>
{
['a'] = new[] { '4', '@' },
['b'] = new[] { '8' },
['c'] = new[] { '(', '{', '[', '<' },
['e'] = new[] { '3' },
['g'] = new[] { '6', '9' },
['i'] = new[] { '1', '!', '|' },
['l'] = new[] { '1', '|', '7' },
['o'] = new[] { '0' },
['s'] = new[] { '$', '5' },
['t'] = new[] { '+', '7' },
['x'] = new[] { '%' },
['z'] = new[] { '2' },
});

/// <summary>
/// Find l33t dictionary matches in <paramref name="password"/>.
/// </summary>
Expand Down
14 changes: 8 additions & 6 deletions zxcvbn-core/Matcher/SpatialMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class SpatialMatcher : IMatcher
private const string ShiftedRegex = "[~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?]";

/// <summary>
/// Gets the spatial graphs to match against.
/// Gets or sets the spatial graphs to match against.
/// </summary>
internal static ReadOnlyCollection<SpatialGraph> SpatialGraphs { get; set; } = GenerateSpatialGraphs();

Expand Down Expand Up @@ -61,11 +61,13 @@ 0 .
0 .
";

return new List<SpatialGraph> { new SpatialGraph("qwerty", qwerty, true),
new SpatialGraph("dvorak", dvorak, true),
new SpatialGraph("keypad", keypad, false),
new SpatialGraph("mac_keypad", macKeypad, false),
}.AsReadOnly();
return new List<SpatialGraph>
{
new SpatialGraph("qwerty", qwerty, true),
new SpatialGraph("dvorak", dvorak, true),
new SpatialGraph("keypad", keypad, false),
new SpatialGraph("mac_keypad", macKeypad, false),
}.AsReadOnly();
}

private static IEnumerable<Matches.Match> SpatialMatch(SpatialGraph graph, string password)
Expand Down

0 comments on commit fa8dec9

Please sign in to comment.