string AttackThread(DatabaseOpener cracker, IPasswordSource passwordSource) { string passwordGuess; while (true) { if (_stopWorking) { return(null); } if (_passwordFound != null) { return(null); } passwordGuess = passwordSource.NextPassword(); if (passwordGuess == null) { return(null); } if (cracker.TryKey(passwordGuess)) { break; } Thread.MemoryBarrier(); _guessCounter++; _lastGuess = passwordGuess; } return(passwordGuess); }
//--- Public Methods --- public void StartAttack(string databasePath, int numThreads, IPasswordSource passwordSource) { if (numThreads <= 0) { throw new ArgumentOutOfRangeException("numThreads"); } lock (_syncLock) { if (_started) { throw new InvalidOperationException(); } _started = true; } if (!PerformSelfTest()) { throw new Exception("Self test failed"); } var fileStream = new FileStream(databasePath, FileMode.Open, FileAccess.Read); DatabaseOpener databaseOpener = new DatabaseOpener(fileStream); stopwatch.Start(); for (int i = 0; i < numThreads; i++) { EasyThread.BeginInvoke(new EasyThreadMethod( () => { Thread.MemoryBarrier(); _guessingThreadsRunning++; Thread.MemoryBarrier(); string result = AttackThread(databaseOpener, passwordSource); Thread.MemoryBarrier(); _guessingThreadsRunning--; Thread.MemoryBarrier(); if (result != null) { _passwordFound = result; OnPasswordFound(new PasswordFoundEventArgs(_passwordFound)); } else if (_guessingThreadsRunning == 0) { OnPasswordNotFound(EventArgs.Empty); } if (_guessingThreadsRunning == 0) { fileStream.Close(); OnCompleted(EventArgs.Empty); } })); } }
//--- Public Methods --- public void StartAttack(string databasePath, int numThreads, IPasswordSource passwordSource) { if (numThreads <= 0) throw new ArgumentOutOfRangeException("numThreads"); lock (_syncLock) { if (_started) throw new InvalidOperationException(); _started = true; } if (!PerformSelfTest()) throw new Exception("Self test failed"); var fileStream = new FileStream(databasePath, FileMode.Open, FileAccess.Read); DatabaseOpener databaseOpener = new DatabaseOpener(fileStream); stopwatch.Start(); for (int i = 0; i < numThreads; i++) { EasyThread.BeginInvoke(new EasyThreadMethod( () => { Thread.MemoryBarrier(); _guessingThreadsRunning++; Thread.MemoryBarrier(); string result = AttackThread(databaseOpener, passwordSource); Thread.MemoryBarrier(); _guessingThreadsRunning--; Thread.MemoryBarrier(); if (result != null) { _passwordFound = result; OnPasswordFound(new PasswordFoundEventArgs(_passwordFound)); } else if (_guessingThreadsRunning == 0) { OnPasswordNotFound(EventArgs.Empty); } if (_guessingThreadsRunning == 0) { fileStream.Close(); OnCompleted(EventArgs.Empty); } })); } }
static void AttackWithPasswordSourceAndWait(string databasePath, int threadCount, IPasswordSource passwordSource) { Cracker cracker = new Cracker(); cracker.PasswordFound += _cracker_PasswordFound; cracker.PasswordNotFound += _cracker_PasswordNotFound; using (var timer = new Timer(TimerCallback, cracker, 3000, 3000)) { cracker.StartAttack(databasePath, threadCount, passwordSource); cracker.WaitUntilCompleted(); } cracker.PasswordFound -= _cracker_PasswordFound; cracker.PasswordNotFound -= _cracker_PasswordNotFound; }
string AttackThread(DatabaseOpener cracker, IPasswordSource passwordSource) { string passwordGuess; while (true) { if (_stopWorking) return null; if (_passwordFound != null) return null; passwordGuess = passwordSource.NextPassword(); if (passwordGuess == null) return null; if (cracker.TryKey(passwordGuess)) break; Thread.MemoryBarrier(); _guessCounter++; _lastGuess = passwordGuess; } return passwordGuess; }