Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance tuning C# permutations and SHA1 code

Tags:

c#

algorithm

linq

Although this is a university assignment (homework) I've come to the best solution I could think of. I would achieve full marks with this code as it matches the question, however I was specially allowed to develop it in C# rather than everyone else using Java, kind of a "yeh, show what c# can do" challenge ;-)

The question was:

Create a program to find the password of a SHA1 hash using a brute force technique, assuming passwords are 6 characters long and can only contain lower-case a-z and 0-9.

I created a LINQ query and after I have the possible combinations I need to run them through SHA1 to get a hash and compare it to the provided password hash.

I created this code:

public static string BruteForceHash(string hash)
        {

                var results = from c0 in Enumerable.Range(0, 36)
                              from c1 in Enumerable.Range(0, 36)
                              from c2 in Enumerable.Range(0, 36)
                              from c3 in Enumerable.Range(0, 36)
                              from c4 in Enumerable.Range(0, 36)
                              from c5 in Enumerable.Range(0, 36)
                              select new string(
                                  new[]
                                {
                                    Characters[c0],
                                    Characters[c1],
                                    Characters[c2],
                                    Characters[c3],
                                    Characters[c4],
                                    Characters[c5],
                                }
                                  );

                string found = null;
                Parallel.ForEach(results, (result, loopstate, a) =>
                                              {
                                                  string hashed = SHA1(result, Encoding.Default);

                                                  if (hashed == hash)
                                                  {
                                                      found = result;
                                                      loopstate.Break();
                                                  }
                                              });

                if (found != null)
                {
                    return found;
                }

            return "Not found.";
        }

Now my real problem is that it solved easy passwords quickly ("aaaaaa" is instant) but obviously takes longer the further the password is away from "aaaaaa".

I would hope someone could provide some pointers on how to increase the performance.

like image 886
Phil Avatar asked Nov 06 '22 03:11

Phil


1 Answers

If you are fairly happy with the implementation then I would run the code with a performance profiler like YourKit or DotTrace you can look at the hot spot in the code and look at tuning from there. Sometimes its nice to do this when you are using syntacticly sugared code like LINQ, that way you can get a feel for whats really going on under the hood too...

like image 109
7sharp9 Avatar answered Nov 09 '22 15:11

7sharp9