How to resolve the algorithm Parallel brute force step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Parallel brute force step by step in the zkl programming language
Table of Contents
Problem Statement
Find, through brute force, the five-letter passwords corresponding with the following SHA-256 hashes: Your program should naively iterate through all possible passwords consisting only of five lower-case ASCII English letters. It should use concurrent or parallel processing, if your language supports that feature. You may calculate SHA-256 hashes by calling a library or through a custom implementation. Print each matching password, along with its SHA-256 hash. Related task: SHA-256
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Parallel brute force step by step in the zkl programming language
Source code in the zkl programming language
var [const] MsgHash=Import.lib("zklMsgHash");
var [const] gotEm=Atomic.Int(); // global signal for all threads
const THREADS=9, // how we will split task, THREADS<=26
CHR_a="a".toAsc();
fcn crack(c,n,hashes){ // thread
sha256:=MsgHash.SHA256; // the SHA-256 hash method, byte bucket
bytes,hash := Data(),Data().howza(0); // byte buckets to reduce garbage production
firstLtrs:=(c+CHR_a).walker(n);
ltrs:=CHR_a.walker; // iterator starting at 97/"a"
foreach a,b,c,d,e in (firstLtrs,ltrs(26),ltrs(26),ltrs(26),ltrs(26)){
if(not hashes2go) return(); // all cracked, stop, not really needed
bytes.clear(a,b,c,d,e); // recycle Data, faster than creating Strings
sha256(bytes,1,hash); // put hash in hash
if(hashes.holds(hash)){
println(bytes.text," --> ",hash.pump(String,"%02x".fmt));
hashes2go.dec(); // I cracked one, let mom thread know
}
}
}
hashes:=T("3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b",
"74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f",
"1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad");
// convert hex strings to binary; cuts down conversions during crack
fcn hex2binary(s){ s.pump(Data,Void.Read,fcn(a,b){ (a+b).toInt(16) }) }
hashes:=hashes.apply(hex2binary);
hashes2go.set(hashes.len()); // number of codes to crack
num,xtra := 26/THREADS, 26%THREADS; // try for the most even spread over threads
s:=0; do(THREADS){ // start threads
n:=num + ((xtra-=1)>=0);
crack.launch(s.toInt(),n,hashes);
s+=n;
}
hashes2go.waitFor(0); // wait until all cracked, just exit, OS kills threads
You may also check:How to resolve the algorithm Least common multiple step by step in the Perl programming language
You may also check:How to resolve the algorithm Stack step by step in the UnixPipes programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Go programming language
You may also check:How to resolve the algorithm Classes step by step in the BQN programming language
You may also check:How to resolve the algorithm Stem-and-leaf plot step by step in the MATLAB / Octave programming language