Skip to content

Instantly share code, notes, and snippets.

@worace
Last active January 4, 2016 04:58
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 worace/8571597 to your computer and use it in GitHub Desktop.
Save worace/8571597 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
class Miner
attr_reader :tree, :parent, :timestamp
def initialize
@tree=`git write-tree`.chomp
@parent=`git rev-parse HEAD`.chomp
@timestamp=12345
prepare_index
end
def prepare_index
user_name = "user-7vxutq41"
`perl -i -pe 's/(#{user_name}: )(\d+)/$1 . ($2+1)/e' LEDGER.txt`
`grep -q "#{user_name}" LEDGER.txt || echo "#{user_name}: 1" >> LEDGER.txt`
`git add LEDGER.txt`
`git update-index --add 100644`
end
def gitify_body_with_header(content)
"commit #{content.length}\0#{content}"
end
def commit_body_with_git(content)
`git hash-object -t commit -w --stdin <<< "#{content.chomp}"`
end
def git_sha(content)
Digest::SHA1.hexdigest(gitify_body_with_header(content))
end
def difficulty
"0001"
end
def commit_body(counter)
body="tree #{tree}
parent #{parent}
author CTF user <me@example.com> #{timestamp} +0000
committer CTF user <me@example.com> #{timestamp} +0000
Give me a Gitcoin
#{counter}
"
end
def generate_sha(counter)
git_sha(commit_body(counter))
end
def run
threads = []
10.times do |i|
threads << Thread.new do
start_time = Time.now
counter = i * 1000000
while true
sha = generate_sha(counter)
counter += 1
if sha < difficulty
#puts "THREAD #{i} got a hit!"
#puts "total time: #{Time.now.to_f - start_time.to_f}"
puts "sha: #{sha}"
#puts "counter: #{counter}"
#puts "difficulty: #{difficulty}"
puts "committing #{commit_body_with_git(commit_body(counter))}"
puts commit_body(counter)
end
if counter > (i+1) * 10000000
break
end
end
end
end
threads.map(&:join)
puts "done"
end
end
Miner.new.run
@wengzilla
Copy link

class Miner
  attr_reader :tree, :parent, :timestamp
  def initialize
    @tree=`git write-tree`.chomp
    @parent=`git rev-parse HEAD`.chomp
    @timestamp=12345
  end

  def gitify_body_with_header(content)
    "commit #{content.length}\0#{content}"
  end

  def git_sha(content)
    Digest::SHA1.hexdigest(gitify_body_with_header(content))
  end

  def difficulty
   "f"
  end

  def generate_body(counter)
    body="tree #{tree}
parent #{parent}
author CTF user <me@example.com> #{timestamp} +0000
committer CTF user <me@example.com> #{timestamp} +0000

Give me a Gitcoin

#{counter}
"
  end

  def run
    threads = []
    1.times do |i|
      threads << Thread.new do
        start_time = Time.now
        counter = i * 10000000
        while true
          #puts "Thread #{i} trying with counter #{counter}"
          body = generate_body(counter)
          sha = git_sha(body)
          counter += 1
          if sha < difficulty
            puts "THREAD #{i} got a hit!"
            puts "total time: #{Time.now.to_f - start_time.to_f}"
            puts "sha: #{sha}"
            puts "counter: #{counter}"
            puts "difficulty: #{difficulty}"
            puts ""
            puts "#{body}"
          end
          if counter > (i+1) * 100000000
            break
          end
        end
      end
    end
    threads.map(&:join)
    puts "done"
  end
end

Miner.new.run

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