linq - Performance tuning C# permutations and SHA1 code -
although university assignment (homework) i've come best solution think of. achieve full marks code matches question, specially allowed develop in c# rather else using java, kind of "yeh, show c# can do" challenge ;-)
the question was:
create program find password of sha1 hash using brute force technique, assuming passwords 6 characters long , can contain lower-case a-z , 0-9.
i created linq query , after have possible combinations need run them through sha1 hash , compare provided password hash.
i created code:
public static string bruteforcehash(string hash) { var results = c0 in enumerable.range(0, 36) c1 in enumerable.range(0, 36) c2 in enumerable.range(0, 36) c3 in enumerable.range(0, 36) c4 in enumerable.range(0, 36) 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 real problem solved easy passwords ("aaaaaa" instant) takes longer further password away "aaaaaa".
i hope provide pointers on how increase performance.
if happy implementation run code performance profiler yourkit or dottrace can @ hot spot in code , @ tuning there. nice when using syntacticly sugared code linq, way can feel whats going on under hood too...
Comments
Post a Comment