Re: Ruby Quiz - Challenge #5 - Crypto Mining - Find the Winning Lucky Number - Nonce (=Number used ONCE) for the Proof-of-Work (PoW) Hash (SHA-256)

"Frank J. Cameron" <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
Gerald Bauer wrote:
> Challenge #5 -Crypto Mining - Find the Winning Lucky Number - Nonce
> (=Number used ONCE) for the Proof-of-Work (PoW) Hash (SHA-256)

$ ruby lib/005.rb ; ruby lib/005.rb ; ruby lib/005.rb
Run options: --seed 10469
# Running:
.
Finished in 0.003223s, 310.3069 runs/s, 620.6138 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
Run options: --seed 50672
# Running:
.
Finished in 0.003319s, 301.2743 runs/s, 602.5487 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
Run options: --seed 19649
# Running:
.
Finished in 0.003258s, 306.9501 runs/s, 613.9003 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips

$ cat lib/005.rb
require_relative '../005/test.rb'
class RubyQuizTest
  def compute_nonce(data)
    0.upto(Float::INFINITY) do |nonce|
      break nonce if sha256("#{nonce}#{data}", 2)==0
    end
  end
  require 'inline'
  inline do |builder|
    builder.include '<string.h>'
    builder.include '<openssl/sha.h>'
    builder.c <<-END
      int sha256(char * s, int difficulty) {
        unsigned char * d = SHA256((unsigned char *)s, strlen(s), 0);
        for(int i=0; i<difficulty; i++) if (d[i] != 0) return -1;
        return 0;
      }
    END
  end
end
RubyQuizTest.new('fjc')



Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.