Parallel brute force: Difference between revisions

Added Wren
m (Added compilation option.)
(Added Wren)
Line 2,193:
apple => 3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b
zyzzx => 1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
This reuses the sha256 function from the [[SHA-256#Wren]] task, though modified to take a byte list argument rather than a string.
 
Wren supports concurrency by way of co-operatively scheduled fibers and, as required by the task description, the work has been divided up between 26 fibers with each fiber taking a different initial letter.
 
However, from an execution speed viewpoint, there is no point in doing so because Wren's VM is single threaded and can only run one fiber at a time. As it takes around 93 seconds on my machine to process a single initial letter (456,976 calls to sha256) this means that an overall runtime of about 40 minutes is needed to find the passwords corresponding to the 3 hashes.
 
Currently, parallel calculations are impossible in Wren-cli. However, if Wren is being embedded, it might be a possible for a suitable host to run several Wren VM's in parallel and divide a task up between them to improve execution time.
<lang ecmascript>import "/fmt" for Fmt
 
var k = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
]
 
var rightRotate = Fn.new { |x, c| (x >> c) | (x << (32 - c)) }
 
var toBytesBE = Fn.new { |val|
var bytes = List.filled(4, 0)
bytes[3] = val & 255
bytes[2] = (val >> 8) & 255
bytes[1] = (val >> 16) & 255
bytes[0] = (val >> 24) & 255
return bytes
}
 
var toIntBE = Fn.new { |bytes| bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3] }
 
var sha256 = Fn.new { |initBytes|
var h0 = 0x6a09e667
var h1 = 0xbb67ae85
var h2 = 0x3c6ef372
var h3 = 0xa54ff53a
var h4 = 0x510e527f
var h5 = 0x9b05688c
var h6 = 0x1f83d9ab
var h7 = 0x5be0cd19
var initLen = initBytes.count
var newLen = initLen + 1
while (newLen % 64 != 56) newLen = newLen + 1
var msg = List.filled(newLen + 8, 0)
for (i in 0...initLen) msg[i] = initBytes[i]
msg[initLen] = 0x80 // remaining bytes already 0
var initBits = initLen * 8
var p = 2.pow(32)
var u = (initBits/p).floor
var l = initBits % p
var bytesU = toBytesBE.call(u)
var bytesL = toBytesBE.call(l)
for (i in 0..3) msg[newLen +i] = bytesU[i]
for (i in 0..3) msg[newLen+i+4] = bytesL[i]
var offset = 0
var w = List.filled(64, 0)
var mask = 0xffffffff
while (offset < newLen) {
for (i in 0..15) w[i] = toIntBE.call(msg[offset+i*4...offset + i*4 + 4])
for (i in 16..63) {
var s0 = rightRotate.call(w[i-15], 7) ^ rightRotate.call(w[i-15], 18) ^ (w[i-15] >> 3)
var s1 = rightRotate.call(w[i- 2], 17) ^ rightRotate.call(w[i- 2], 19) ^ (w[i- 2] >> 10)
w[i] = w[i-16] + s0 + w[i-7] + s1
}
var a = h0
var b = h1
var c = h2
var d = h3
var e = h4
var f = h5
var g = h6
var h = h7
for (i in 0..63) {
var s1 = rightRotate.call(e, 6) ^ rightRotate.call(e, 11) ^ rightRotate.call(e, 25)
var ch = (e & f) ^ ((~e) & g)
var temp1 = h + s1 + ch + k[i] + w[i]
var s0 = rightRotate.call(a, 2) ^ rightRotate.call(a, 13) ^ rightRotate.call(a, 22)
var maj = (a & b) ^ (a & c) ^ (b & c)
var temp2 = s0 + maj
h = g
g = f
f = e
e = d + temp1
d = c
c = b
b = a
a = temp1 + temp2
}
h0 = (h0 + a) & mask
h1 = (h1 + b) & mask
h2 = (h2 + c) & mask
h3 = (h3 + d) & mask
h4 = (h4 + e) & mask
h5 = (h5 + f) & mask
h6 = (h6 + g) & mask
h7 = (h7 + h) & mask
offset = offset + 64
}
h0 = Fmt.swrite("$08x", h0)
h1 = Fmt.swrite("$08x", h1)
h2 = Fmt.swrite("$08x", h2)
h3 = Fmt.swrite("$08x", h3)
h4 = Fmt.swrite("$08x", h4)
h5 = Fmt.swrite("$08x", h5)
h6 = Fmt.swrite("$08x", h6)
h7 = Fmt.swrite("$08x", h7)
return h0 + h1 + h2 + h3 + h4 + h5 + h6 + h7
}
 
var hashes = [
"1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad",
"3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b",
"74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f"
]
 
var findHash = Fn.new { |i|
var bytes = List.filled(5, 0)
var r = 97..122
bytes[0] = i
for (j in r) {
bytes[1] = j
for (k in r) {
bytes[2] = k
for (l in r) {
bytes[3] = l
for (m in r) {
bytes[4] = m
var d = sha256.call(bytes)
for (hash in hashes) {
if (hash == d) {
var s = bytes.map { |b| String.fromByte(b) }.join()
System.print("%(s) => %(d)")
hashes.remove(hash)
if (hashes.count == 0) return
}
}
}
}
}
}
}
 
for (i in 0..25) {
var fib = Fiber.new(findHash)
fib.call(97+i)
}</lang>
 
{{out}}
<pre>
apple => 3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b
mmmmm => 74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f
zyzzx => 1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad
</pre>
 
=={{header|zkl}}==
9,476

edits