Anonymous user
Parallel brute force: Difference between revisions
m
→{{header|Haskell}}
Line 753:
</ul>
<br/>
Compile with "-O2 -threaded -rtsopts". Run with "+RTS -N<number of cores>"
<lang haskell>import Crypto.Hash (hashWith, SHA256 (..), Digest)
import Control.Monad (replicateM, join)
import Control.Monad.Par (runPar, get, spawnP)
import Control.Concurrent (getNumCapabilities)
import Data.Text (Text, pack)
import Data.Text.Encoding (encodeUtf8)
Line 771 ⟶ 774:
wordVariations = pack <$> replicateM 5 ['a'..'z']
bruteForce :: Int -> [Text] -> [(String, Text)]
bruteForce n xs = runPar $ do
pars <- mapM (spawnP . foldr findMatch []) chunks
result <- mapM get pars
pure $ join result
where
chunks = chunksOf (26 ^ 5 `div`
findMatch s accum
| hashed `elem` hashedValues = (show hashed, s) : accum
Line 785 ⟶ 788:
main :: IO ()
main = do
main = mapM_ (uncurry (printf "%s -> %s\n")) (bruteForce wordVariations)</lang>▼
cores <- getNumCapabilities
printf "Using %d cores\n" cores
{{out}}
<pre>brute +RTS -N2 -s
<pre>3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b -> apple▼
Using 2 cores
74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f -> mmmmm
1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad -> zyzzx
|