Created
May 31, 2017 15:05
-
-
Save lifthrasiir/771b08eca2a29bbcdde8e1fc1a18e455 to your computer and use it in GitHub Desktop.
Brute forcing solver for the Illustration Power Level Calculator https://solarias.github.io/dnf/power_level.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import string | |
import md5 | |
import base64 | |
def power_level(s): | |
return digest(md5.md5(base64.b64encode(s))) | |
def digest(m): | |
h = bytearray(m.digest()) | |
v = 1 | |
v *= h[ 0] >> 4; v += h[ 0] & 15; v += h[ 1] >> 4; v += h[ 1] & 15 | |
v *= h[ 2] >> 4; v += h[ 2] & 15; v += h[ 3] >> 4; v += h[ 3] & 15 | |
v *= h[ 4] >> 4; v += h[ 4] & 15; v += h[ 5] >> 4; v += h[ 5] & 15 | |
v *= h[ 6] >> 4; v += h[ 6] & 15; v += h[ 7] >> 4; v += h[ 7] & 15 | |
v *= h[ 8] >> 4; v += h[ 8] & 15; v += h[ 9] >> 4; v += h[ 9] & 15 | |
v *= h[10] >> 4; v += h[10] & 15; v += h[11] >> 4; v += h[11] & 15 | |
v *= h[12] >> 4; v += h[12] & 15; v += h[13] >> 4; v += h[13] & 15 | |
v *= h[14] >> 4; v += h[14] & 15; v += h[15] >> 4; v += h[15] & 15 | |
return v | |
def brute(s): | |
maxv = power_level(s) | |
print >>sys.stderr, 'baseline:', maxv | |
alpha = string.ascii_letters + string.digits + '+/' | |
assert len(alpha) == 64 | |
while len(s) % 3 != 0: s += '\0' | |
ss = base64.b64encode(s) | |
assert not ss.endswith('='), 'unexpected padding' | |
h = md5.md5(ss) | |
v = digest(h) | |
if maxv < v: | |
maxv = v | |
print >>sys.stderr, 'new frontier: %r = %d' % ('', maxv) | |
''' | |
for i in alpha: | |
for j in alpha: | |
for k in alpha: | |
for l in alpha: | |
hh = h.copy() | |
hh.update(i + j + k + l) | |
v = digest(hh) | |
if maxv < v: | |
maxv = v | |
print >>sys.stderr, 'new frontier: %r = %d' % (i + j + k + l, v) | |
''' | |
for ii in alpha: | |
for jj in alpha: | |
for kk in alpha: | |
for ll in alpha: | |
hh = h.copy() | |
hh.update(ii + jj + kk + ll) | |
for i in alpha: | |
for j in alpha: | |
for k in alpha: | |
for l in alpha: | |
hhh = hh.copy() | |
hhh.update(i + j + k + l) | |
v = digest(hhh) | |
if maxv < v: | |
maxv = v | |
print >>sys.stderr, 'new frontier: %r = %d' % (ii + jj + kk + ll + i + j + k + l, v) | |
if __name__ == '__main__': | |
s = sys.stdin.read() | |
brute(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment