Commit c313f829 authored by Rob Nelson's avatar Rob Nelson

Add 1337 support.

parent 37df47b5
......@@ -49,7 +49,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>MurrayGrant.snk</AssemblyOriginatorKeyFile>
......@@ -74,6 +74,9 @@
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
......@@ -126,7 +129,7 @@
<PropertyGroup>
<PostBuildEvent>copy /y "$(SolutionDir)MergePartsOfSpeech\bin\$(ConfigurationName)\dictionary.xml" "$(TargetDir)"
del "$(TargetDir)dictionary.xml.gz"
"$(SolutionDir)gzip.exe" -9 "$(TargetDir)dictionary.xml"</PostBuildEvent>
gzip -9 "$(TargetDir)dictionary.xml"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
......
......@@ -61,6 +61,7 @@ namespace MurrayGrant.ReadablePassphrase.Generator
}
RunMain();
Console.ReadKey();
Environment.Exit(0);
}
catch (Exception ex)
......@@ -138,7 +139,7 @@ namespace MurrayGrant.ReadablePassphrase.Generator
Console.WriteLine("Total combinations {0:E3} - {1:E3} ({2:N2} - {3:N2} bits)", combinations.Shortest, combinations.Longest, combinations.ShortestAsEntropyBits, combinations.LongestAsEntropyBits);
if (applyStandardMutators)
Console.WriteLine("Using standard mutators (2 numbers, 2 capitals)");
Console.WriteLine("Using standard mutators (2 leet numbers, 2 capitals)");
else if (!applyStandardMutators && numericStyle != 0 && upperStyle != 0)
Console.WriteLine("Using upper case and numeric mutators ({0:N0} capital(s), {1:N0} number(s))", upperCount, numericCount);
else if (!applyStandardMutators && numericStyle == 0 && upperStyle != 0)
......@@ -155,7 +156,7 @@ namespace MurrayGrant.ReadablePassphrase.Generator
int generated = 0;
int attempts = 0;
int maxAttempts = count * MaxAttemptsPerCount;
var mutators = applyStandardMutators ? new IMutator[] { UppercaseMutator.Basic, NumericMutator.Basic } : Enumerable.Empty<IMutator>();
var mutators = applyStandardMutators ? new IMutator[] { UppercaseMutator.Basic, LeetMutator.Basic } : Enumerable.Empty<IMutator>();
if (upperStyle != 0)
mutators = mutators.Concat(new IMutator[] { new UppercaseMutator() { When = upperStyle, NumberOfCharactersToCapitalise = upperCount } });
if (numericStyle != 0)
......
......@@ -39,6 +39,8 @@ namespace KeePassReadablePassphrase
public int UpperCount { get; set; }
public NumericStyles NumericStyle { get; set; }
public int NumericCount { get; set; }
public NumericStyles LeetStyle { get; set; }
public int LeetCount { get; set; }
public Config()
{
......@@ -64,6 +66,8 @@ namespace KeePassReadablePassphrase
this.UpperCount = UppercaseMutator.Basic.NumberOfCharactersToCapitalise;
this.NumericStyle = NumericMutator.Basic.When;
this.NumericCount = NumericMutator.Basic.NumberOfNumbersToAdd;
this.LeetStyle = LeetMutator.Basic.When;
this.LeetCount = LeetMutator.Basic.NumberOfCharactersToMutate;
}
private void ParseConfig(string configFromKeePass)
{
......@@ -103,6 +107,10 @@ namespace KeePassReadablePassphrase
this.NumericStyle = (NumericStyles)Enum.Parse(typeof(NumericStyles), reader.GetAttribute("value").Replace(" ", ""));
else if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "numericcount")
this.NumericCount = Int32.Parse(reader.GetAttribute("value"));
else if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "leetstyle")
this.LeetStyle = (NumericStyles)Enum.Parse(typeof(NumericStyles), reader.GetAttribute("value").Replace(" ", ""));
else if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "leetcount")
this.LeetCount = Int32.Parse(reader.GetAttribute("value"));
}
if (this.PhraseStrength != PhraseStrength.Custom)
......@@ -125,6 +133,10 @@ namespace KeePassReadablePassphrase
NumericCount = 0;
if (NumericCount > 999)
NumericCount = 999;
if (LeetCount < 0)
LeetCount = 0;
if (LeetCount > 999)
LeetCount = 999;
}
public string ToConfigString()
{
......@@ -152,6 +164,8 @@ namespace KeePassReadablePassphrase
sb.AppendFormat("<UpperCount value=\"{0}\"/>\n", this.UpperCount);
sb.AppendFormat("<NumericStyle value=\"{0}\"/>\n", this.NumericStyle);
sb.AppendFormat("<NumericCount value=\"{0}\"/>\n", this.NumericCount);
sb.AppendFormat("<LeetStyle value=\"{0}\"/>\n", this.LeetStyle);
sb.AppendFormat("<LeetCount value=\"{0}\"/>\n", this.LeetCount);
sb.AppendLine("</ReadablePassphraseConfig>");
return sb.ToString();
}
......
......@@ -199,6 +199,9 @@ namespace KeePassReadablePassphrase
this.cboNumericStyle.DataSource = Enum.GetNames(typeof(NumericStyles));
this.cboNumericStyle.Text = config.NumericStyle.ToString();
this.nudNumberCount.Value = config.NumericCount;
this.cboLeetStyle.DataSource = Enum.GetNames(typeof(NumericStyles));
this.cboLeetStyle.Text = config.LeetStyle.ToString();
this.nudLeetCount.Value = config.LeetCount;
this.UpdateDescription(config);
......@@ -275,6 +278,8 @@ namespace KeePassReadablePassphrase
this.nudUpperCount.Enabled = this.radMutatorCustom.Checked;
this.cboNumericStyle.Enabled = this.radMutatorCustom.Checked;
this.nudNumberCount.Enabled = this.radMutatorCustom.Checked;
this.cboLeetStyle.Enabled = this.radMutatorCustom.Checked;
this.nudLeetCount.Enabled = this.radMutatorCustom.Checked;
}
private Config FormToConfigObject()
......@@ -309,6 +314,8 @@ namespace KeePassReadablePassphrase
result.UpperCount = (int)this.nudUpperCount.Value;
result.NumericStyle = (NumericStyles)Enum.Parse(typeof(NumericStyles), this.cboNumericStyle.Text);
result.NumericCount = (int)this.nudNumberCount.Value;
result.LeetStyle = (NumericStyles)Enum.Parse(typeof(NumericStyles), this.cboLeetStyle.Text);
result.LeetCount = (int)this.nudLeetCount.Value;
return result;
}
......
......@@ -189,11 +189,12 @@ namespace KeePassReadablePassphrase
if (conf.Mutator == MutatorOption.None)
return Enumerable.Empty<IMutator>();
else if (conf.Mutator == MutatorOption.Standard)
return new IMutator[] { UppercaseMutator.Basic, NumericMutator.Basic };
return new IMutator[] { UppercaseMutator.Basic, NumericMutator.Basic, LeetMutator.Basic };
else if (conf.Mutator == MutatorOption.Custom)
return new IMutator[] {
new UppercaseMutator() { When = conf.UpperStyle, NumberOfCharactersToCapitalise = conf.UpperCount },
new NumericMutator() { When = conf.NumericStyle, NumberOfNumbersToAdd = conf.NumericCount },
new LeetMutator() { When = conf.LeetStyle, NumberOfCharactersToMutate = conf.LeetCount },
};
else
return Enumerable.Empty<IMutator>();
......
......@@ -36,10 +36,10 @@
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>MurrayGrant.snk</AssemblyOriginatorKeyFile>
<AssemblyOriginatorKeyFile>NotMurrayGrant.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
......
// Copyright 2014 Rob Nelson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using MurrayGrant.ReadablePassphrase.Random;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MurrayGrant.ReadablePassphrase.Mutators
{
/// <summary>
/// Changes letters to a look-alike number, as used in "1337speak".
/// </summary>
public class LeetMutator : ICombinations, IMutator
{
/// <summary>
/// A general instance designed to get a phrase to pass password requirements more than add serious entropy.
/// </summary>
public readonly static LeetMutator Basic = new LeetMutator()
{
When = NumericStyles.Anywhere,
NumberOfCharactersToMutate = 2
};
public readonly static Dictionary<char, char> LeetMap = new Dictionary<char, char>() {
{'a','4'},
{'e','3'},
{'g','6'},
{'l','1'},
{'o','0'},
{'r','2'},
{'s','5'},
};
public int NumberOfCharactersToMutate {get;set;}
public NumericStyles When { get; set; }
public double CalculateExtraCombinations()
{
return double.NaN;
}
public void Mutate(StringBuilder passphrase, RandomSourceBase random)
{
if (this.When == NumericStyles.Never || this.NumberOfCharactersToMutate <= 0)
return;
// Make a list of positions which can have numbers inserted.
var possibleInsertIndexes = new List<int>();
for (int i = 0; i <= passphrase.Length; i++)
{
if (
((this.When & NumericStyles.Anywhere) == NumericStyles.Anywhere)
|| ((this.When & NumericStyles.StartOfWord) == NumericStyles.StartOfWord &&
((i == 0) || (i > 0 && i < passphrase.Length && Char.IsWhiteSpace(passphrase[i - 1]) && Char.IsLetterOrDigit(passphrase[i])))
)
|| ((this.When & NumericStyles.EndOfWord) == NumericStyles.EndOfWord &&
(i < passphrase.Length && Char.IsWhiteSpace(passphrase[i]) && Char.IsLetterOrDigit(passphrase[i - 1]))
)
)
if (i > 0 && i < passphrase.Length && LeetMap.ContainsKey(passphrase[i]))
possibleInsertIndexes.Add(i);
}
// Usually, there's a trailing whitespace character, but just in case there isn't...
if ((this.When & NumericStyles.EndOfWord) == NumericStyles.EndOfWord && !Char.IsWhiteSpace(passphrase[passphrase.Length - 1]))
if (LeetMap.ContainsKey(passphrase[passphrase.Length-1]))
possibleInsertIndexes.Add(passphrase.Length-1);
// Randomly choose up to the count allowed.
var toInsertAt = new List<int>();
var c = Math.Min(this.NumberOfCharactersToMutate, possibleInsertIndexes.Count);
while (c > 0)
{
var idx = random.Next(possibleInsertIndexes.Count);
toInsertAt.Add(possibleInsertIndexes[idx]);
possibleInsertIndexes.RemoveAt(idx);
c--;
}
// Actually insert numbers.
// Because we're inserting, we do it in reverse order of index, so we don't need to adjust indexes.
toInsertAt.Sort();
toInsertAt.Reverse();
foreach (var idx in toInsertAt)
passphrase[idx]= LeetMap[passphrase[idx]];
}
}
}
......@@ -30,8 +30,12 @@ namespace MurrayGrant.ReadablePassphrase.Mutators
/// </summary>
public readonly static NumericMutator Basic = new NumericMutator()
{
/*
When = NumericStyles.EndOfWord,
NumberOfNumbersToAdd = 2,
*/
When = NumericStyles.Never,
NumberOfNumbersToAdd = 0
};
public NumericStyles When { get; set; }
......
......@@ -36,7 +36,7 @@
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>MurrayGrant.snk</AssemblyOriginatorKeyFile>
......@@ -86,6 +86,7 @@
<Compile Include="MaterialisedWords\Preposition.cs" />
<Compile Include="MaterialisedWords\Verb.cs" />
<Compile Include="Mutators\ICombinations.cs" />
<Compile Include="Mutators\LeetMutator.cs" />
<Compile Include="Mutators\NullMutator.cs" />
<Compile Include="Mutators\NumericMutator.cs" />
<Compile Include="Mutators\UppercaseMutator.cs" />
......
File deleted
File deleted
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment