Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created August 5, 2012 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takehiko/3266781 to your computer and use it in GitHub Desktop.
Save takehiko/3266781 to your computer and use it in GitHub Desktop.
Find all-zero hash
#!/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
@cab404
Copy link

cab404 commented Sep 19, 2016

did you succeed tho? :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment