Skip to content

Instantly share code, notes, and snippets.

@vhoyer
Last active November 16, 2017 19:06
Show Gist options
  • Save vhoyer/6ad3f3c8d691d1c98f50c9ae8acb777f to your computer and use it in GitHub Desktop.
Save vhoyer/6ad3f3c8d691d1c98f50c9ae8acb777f to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Linq;
using System.Threading;
namespace KeyGenerator {
class Program {
static readonly char[] valid = "0123456789abcdefghijklmnopqrstuvxywzABCDEFGHIJKLMNOPQRSTUVXYWZ".ToCharArray();
static string lastKey = String.Format("{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}", valid[0]);
static double lastIndex = 0;
static int match = 25000000;
static string path;
static System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
static void Main(string[] args) {
if(args.Length == 1) {
match = int.Parse(args[0]);
}
string keypath = Environment.CurrentDirectory + "/keys"+match+".txt";
if(!File.Exists(keypath))
File.Create(keypath).Close();
path = Environment.CurrentDirectory + "/keys"+match+".dat";
if(!File.Exists(path))
File.Create(path).Close();
else
try {
string lastLine = File.ReadLines(path).Last();
lastIndex = ulong.Parse(lastLine.Split('\t')[0]);
lastKey = lastLine.Split('\t')[1];
}
catch(InvalidOperationException) { }
Console.WriteLine(path);
stopwatch.Start();
while(lastKey != "ZZZZZZZZZZZZZZZZZZZZ") {
lastKey = generateNextKey(lastKey);
Console.WriteLine(lastKey + "\t: "+lastIndex);
using(StreamWriter sw = File.AppendText(keypath)) {
sw.Write(lastIndex+"\t"+lastKey+"\n");
}
}
}
private static string generateNextKey(string lastKey) {
byte[] i = Array.ConvertAll(lastKey.ToCharArray(), x => (byte)x);
i = iterateThrough(i);
Func<string> compile = () => String.Join("", Array.ConvertAll(i, x => (char)x));
Func<int> calc = () => ((i[0] + i[1] + i[2] + i[3]) * (i[4] + i[5] + i[6] + i[7]) + (i[8] + i[9]
+ i[10] + i[11]) * (i[12] + i[13] + i[14] + i[15])) * (i[16] + i[17] + i[18] + i[19]);
Func<bool> test = () => calc() == match;
double j,maxIter = Math.Pow(256, 20) - 1;
for(j = lastIndex + 1; j < maxIter; j++) {
if(j % 1000000 == 0) {
Console.WriteLine("{0}\t: {1}\t{2}%\t={3}\t[{4}ms]",compile(),j,(j / maxIter * 100),calc(),stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
}
if(j % 10000000 == 0) {
double progressJ = j;
string progressKey = compile();
new Thread(() => {//saveProgress
using(StreamWriter sw = new StreamWriter(path, false))
sw.Write(progressJ + "\t" + progressKey + "\r\n");
}).Start();
}
if(test()) break;
i = iterateThrough(i);
}
lastIndex = j;
return compile();
}
private static byte[] iterateThrough(byte[] i) {
for(int s = 0; s < valid.Length; s++) {
int nextIndex = Array.FindIndex(valid, x => i[s] == x) + 1;
i[s] = (byte)valid[nextIndex > valid.Length - 1 ? 0 : nextIndex];
if(i[s] != valid[0]) break;
}
return i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment