So I have a .txt file with 30million lines for which I hash each with MD5 and SHA1 and insert it into my MySQL database. It works completely fine, but it takes 1 second for each hash. That's 30 million seconds...
The problem: Its not fast enough, 1 per 1 second is too slow.
What I want: Some kind of threading that can speed it up so it does like 20 in 1 second! (maybe that's too unrealistic)
Here's my code, feel free to manipulate: (I'm using MySql.Data as a reference)
bool success;
int done = 0;
string command;
WebClient putHash = new WebClient();
string server = "my_server";
string database = "my_db";
string uid = "my_dbuser";
string password = "my_pass";
string connectionstring = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + uid + ";PASSWORD=" + password + ";";
MySqlConnection cnn = new MySqlConnection(connectionstring);
MySqlCommand cmd = new MySqlCommand();
foreach (string line in File.ReadLines(@"directory with .txt file which has 30million lines"))
{
string linefixed = line.Replace(" ", "").Replace("'", "").Replace(";", "").Replace(")", "").Replace("\\", "").Replace("=", "");
success = false;
byte[] hashedv = new UTF8Encoding().GetBytes(linefixed);
byte[] hash = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(hashedv);
string encodedinput = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();
byte[] bytes = Encoding.UTF8.GetBytes(linefixed);
var sha1 = SHA1.Create();
byte[] hashBytes = sha1.ComputeHash(bytes);
byte[] hexed = hashBytes;
var sb = new StringBuilder();
foreach (byte b in hexed)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
string sha1done = sb.ToString();
while (!success)
{
try
{
command = "INSERT INTO passwdhashes (password,MD5,SHA1) VALUES('" + linefixed + "','" + encodedinput + "','" + sha1done + "')" ;
cmd.CommandText = command;
cmd.Connection = cnn;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
success = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
cnn.Close();
}
}
done = done + 1;
Console.WriteLine("\n" + done);
}
Console.ReadKey();
}
}
}