Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A naive approach for randomly trying to find the smallest SHA512.
require 'OpenSSL'
#require 'SecureRandom'
class String
def to_binary
unpack("S>*").map { |c|
v = 0
if (c & 0x60) == 0x60
v += (c & 0xff) - 0x57
else #0x30
v += (c & 0xf)
end
if (c & 0x6000) == 0x6000
v += ((c & 0xff00) >> 4) - (0x57 << 4)
else #0x3000
v += ((c & 0xf00) >> 4)
end
v
}.pack("C*")
end
def to_js
"javascript:document.getElementById(\"string\").value=\"#{gsub(/(.{2})/, '\x\1')}\";"
end
def valid_submission
gsub(/(.{2})/, '\x\1')[/\\x(00|01|02|03|04|05|06|07|08|0a|0b|0c|0d|0e|0f|10|11|12|13|14|15|16|17|18|19|1a|1b|1c|1d|1e|1f)/] == nil
end
end
#module SecureRandom
# def valid_hex(n)
# while !(h = SecureRandom.hex(n)).valid_submission
# sleep 0.01
# end
# h
# end
# module_function :valid_hex
#end
def random_bytes(n) #quicker, just forget about 0x09 to simplify it
s = ""
n.times { s << "0123456789abcdef"[rand(2..15)] << "0123456789abcdef"[rand(2..15)] }
s
end
lowest = "d95eec2bf2a56454c669852d97469ab4ddae84e89c796a7656a8f459d49a4ef9e9fd95ad47f66ff59582722994b4466453fea26bfbe255d592c64b9fce358f"
lowest_hash = OpenSSL::Digest::SHA512.hexdigest(lowest.to_binary)
cur = random_bytes(rand(20..128)) #SecureRandom.valid_hex(rand(20..128))
trap("SIGINT") {
puts "\nstring: " << lowest
puts "hash: " << lowest_hash
puts "JS: " << lowest.to_js
puts "current: " << cur
puts "hash: " << OpenSSL::Digest::SHA512.hexdigest(cur.to_binary)
exit
}
loop {
if lowest_hash > cur_hash = OpenSSL::Digest::SHA512.hexdigest(cur.to_binary)
puts "#{lowest_hash[0..12]} -> #{cur_hash[0..12]} (#{cur})"
lowest = cur
lowest_hash = cur_hash
end
sleep 0.001
cur = random_bytes(rand(20..128)) #SecureRandom.valid_hex(rand(20..128))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.