Skip to content

Commit

Permalink
Merge pull request #8 from ynuwenhof/main
Browse files Browse the repository at this point in the history
Code quality improvements
  • Loading branch information
vdohney authored May 20, 2023
2 parents f49ed5d + 7189331 commit 5d5dda4
Showing 1 changed file with 61 additions and 54 deletions.
115 changes: 61 additions & 54 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,50 @@
// usage:
// dotnet run <path_to_dump>

using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace keepass_password_dumper;

class Program
internal static class Program
{
// What characters are valid password characters
const string allowedChars = "^[\x20-\x7E]+$";
private const string AllowedChars = "^[\x20-\x7E]+$";

// Read file in N-sized chunks
const int bufferSize = 524288; //2^19
static string passwordChar = "●";
private const int BufferSize = 524288; //2^19

static void Main(string[] args)
private static void Main(string[] args)
{
// Windows terminal has issues displaying "●"
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
passwordChar = "*";
}
var passwordChar = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "*" : "●";

if (args.Length != 1)
{
Console.WriteLine("Please specify a file path as an argument.");
return;
}

string filePath = args[0];
var filePath = args[0];
if (!File.Exists(filePath))
{
Console.WriteLine("File not found.");
return;
}

Dictionary<int, HashSet<string>> candidates = new Dictionary<int, HashSet<string>>();
var candidates = new Dictionary<int, HashSet<string>>();

int currentStrLen = 0;
string debugStr = "";
var currentStrLen = 0;
var debugStr = string.Empty;

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[bufferSize];
var buffer = new byte[BufferSize];
int bytesRead;

while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
for (int i = 0; i < bytesRead - 1; i++)
for (var i = 0; i < bytesRead - 1; i++)
{
// ● = 0xCF 0x25
if (buffer[i] == 0xCF && buffer[i + 1] == 0x25)
Expand All @@ -60,38 +59,42 @@ static void Main(string[] args)
}
else
{
if (currentStrLen != 0)
if (currentStrLen == 0) continue;

currentStrLen++;

string strChar;
try
{
currentStrLen++;
var character = new[] { buffer[i], buffer[i + 1] };
strChar = System.Text.Encoding.Unicode.GetString(character);
}
catch
{
continue;
}

string strChar = "";
try
var isValid = Regex.IsMatch(strChar, AllowedChars);

if (isValid)
{
// Convert to UTF 8
if (!candidates.ContainsKey(currentStrLen))
{
byte[] character = new byte[] { buffer[i], buffer[i + 1] };
strChar = System.Text.Encoding.Unicode.GetString(character);
candidates.Add(currentStrLen, new HashSet<string> { strChar });
}
catch { continue; }

bool isValid = Regex.IsMatch(strChar, allowedChars);

if (isValid)
else
{
// Convert to UTF 8
if (!candidates.ContainsKey(currentStrLen))
candidates.Add(currentStrLen, new HashSet<string>() { strChar });
else
{
if (!candidates[currentStrLen].Contains(strChar))
candidates[currentStrLen].Add(strChar);
}

debugStr += strChar;
Console.WriteLine("Found: " + debugStr);
if (!candidates[currentStrLen].Contains(strChar))
candidates[currentStrLen].Add(strChar);
}

currentStrLen = 0;
debugStr = "";
debugStr += strChar;
Console.WriteLine($"Found: {debugStr}");
}

currentStrLen = 0;
debugStr = "";
}
}
}
Expand All @@ -102,34 +105,38 @@ static void Main(string[] args)
Console.WriteLine($"Unknown characters are displayed as \"{passwordChar}\"");

Console.WriteLine($"1.:\t{passwordChar}");
string combined = passwordChar;
int count = 2;
foreach (KeyValuePair<int, HashSet<string>> kvp in candidates.OrderBy(x => x.Key))
var combined = passwordChar;
var count = 2;

foreach (var (key, value) in candidates.OrderBy(x => x.Key))
{
while (kvp.Key > count)
while (key > count)
{
Console.WriteLine($"{count}.:\t{passwordChar}");
combined += passwordChar;
count++;
}
}

Console.Write($"{kvp.Key}.:\t");
if (kvp.Value.Count != 1)
Console.Write($"{key}.:\t");
if (value.Count != 1)
combined += "{";
foreach (string c in kvp.Value)

foreach (var c in value)
{
Console.Write($"{c}, ");

combined += c;
if (kvp.Value.Count != 1)
if (value.Count != 1)
combined += ", ";
}
if (kvp.Value.Count != 1)
combined = combined.Substring(0, combined.Length - 2) + "}";

if (value.Count != 1)
combined = combined[..^2] + "}";

Console.WriteLine();
count++;
}
Console.WriteLine("Combined: " + combined);

Console.WriteLine($"Combined: {combined}");
}
}
}

0 comments on commit 5d5dda4

Please sign in to comment.