Created
August 5, 2012 19:21
-
-
Save takehiko/3266781 to your computer and use it in GitHub Desktop.
Find all-zero hash
This file contains hidden or 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
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
class StringAndHash | |
def initialize(s, h) | |
@string = s | |
@hash = h | |
@length_zero = (/^0+/ =~ @hash) ? $&.length : 0 | |
end | |
attr_reader :length_zero | |
def to_s | |
"#{@hash} (#{length_zero}) #{@string}" | |
end | |
end | |
class HashAllZero | |
def initialize(string_init = "a", digest = :md5) | |
@string = string_init.dup | |
@digest = digest | |
@length_max = 0 | |
require "digest/#{@digest}" | |
case @digest | |
when :md5 | |
@dclass = Digest::MD5 | |
when :sha1 | |
@dclass = Digest::SHA1 | |
else | |
die "#{@digest}: no such digest name" | |
end | |
end | |
attr_reader :string | |
def start | |
while true | |
d = @dclass.hexdigest(@string) | |
if d[0, 1] == "0" | |
sh = StringAndHash.new(@string, d) | |
if sh.length_zero > @length_max | |
puts sh | |
@length_max = sh.length_zero | |
end | |
end | |
@string.succ! | |
end | |
end | |
end | |
if __FILE__ == $0 | |
haz = HashAllZero.new(ARGV.shift || "a") | |
while true | |
begin | |
haz.start | |
rescue Interrupt | |
puts "[interrupt] trying #{haz.string}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
did you succeed tho? :D