Bitcoin Forum

Other => Beginners & Help => Topic started by: sjors on May 09, 2013, 11:47:30 PM



Title: How do I perform the SHA-256 hashing on a public key?
Post by: sjors on May 09, 2013, 11:47:30 PM
I'm trying to understand bitcoin better by trying things out in Ruby. I know some aspects of Bitcoin have already been implemented (https://github.com/lian/bitcoin-ruby) in Ruby, but I'm trying to avoid "cheating" by studying those.

I read the wiki regarding scripts (https://en.bitcoin.it/wiki/Script) as well as addresses (https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses) and I'm confused as to how to convert between the public key and the bitcoin address.

The scriptPubKey of a transaction output usually looks like this: OP_DUP OP_HASH160 <pubKeyHash>
OP_EQUALVERIFY OP_CHECKSIG. If I understand correctly, this <pubKeyHash> is the result of Base58 decoding the bitcoin address.

In ruby that can be done like this ("step 9" from the address page results in "step 8"):
Code:
require 'base58_gmp'
Base58GMP.decode("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", "bitcoin").to_s(16)

So far so good (although it took a long time to realize bitcoin uses a non-standard form of Base58).

The scriptSig of a transaction input looks this this: <sig> <pubKey>. If I understand correctly <pubKey> here is the thing shown in step 1: "04508 ... 82BA6".

Now according to the scripts page, OP_HASH(<pubkey>) should result in <pubKeyHash>, but I'm having a hard time achieving that. OP_HASH is supposed to (https://en.bitcoin.it/wiki/Script#Crypto) only perform a SHA256 hash followed by an RMB160 hash. That looks like step 2 and step 3, but does not include step 4 through 8 (adding network info and a checksum). My guess is that I also need to follow those steps when I see OP_HASH.

Unfortunately I'm already stuck at step 2. If I try to reproduce the example in ruby, I get different results for the SHA256 hash.
Code:
require 'digest/sha2'
pubkey = "0450863A....582BA6"
step_2 = (Digest::SHA2.new << pubKey).to_s
That results in "32511e82d56...."  in stead of "600FFE422..." as it says on the wiki. Both are 64 characters.

What am I doing wrong?


By the way, I tried searching the forum for similar questions, but search is blocked for several minutes after one or two attempts...


Title: Re: How do I perform the SHA-256 hashing on a public key?
Post by: sjors on May 10, 2013, 10:10:21 AM
Alright, I had to cheat here and peek at bitcoin-ruby. I needed to replace:

Code:
step_2 = (Digest::SHA2.new << pubKey).to_s

With this, by adding pack("H*"):

Code:
step_2 = (Digest::SHA2.new << [pubKey].pack("H*")).to_s

The problem was that my code created a sha256 digest of a string and not of the actual hex value. This meant for instance that there was a difference between the digest of pubKey.downcase and pubKey.upcase.


Title: Re: How do I perform the SHA-256 hashing on a public key?
Post by: Vergil77 on July 04, 2013, 02:01:11 PM
what can i do?

Startseite
Charts
Statistiken
Markets
Entwickler
Wallet
 

Unverbrauchten Ausgänge 1Bv82xxr341Uf7hZfWPy9RiNUkG4hx3CMf
Total: 1.33 BTC
a31ee8dc42faf6f06ee609a8fa25b69c3eab2768ff3bc455879ee2f45087e0d12013-07-02 14:13:34
OP_DUP OP_HASH160 77bd2ce6db4564c6a640fccd133da6371f89fd96 OP_EQUALVERIFY OP_CHECKSIG (1Bv82xxr341Uf7hZfWPy9RiNUkG4hx3CMf)
344 Bestätigungen  0.6 BTC
242ed4f0f7986ecc13f27ce01d294e22935288ee0c6018e05763813d34b076882013-06-30 13:55:34
OP_DUP OP_HASH160 77bd2ce6db4564c6a640fccd133da6371f89fd96 OP_EQUALVERIFY OP_CHECKSIG (1Bv82xxr341Uf7hZfWPy9RiNUkG4hx3CMf)
653 Bestätigungen  0.33 BTC
2addf8699726fd7def844e9cec615d610a4ff3eb50b0cdd47ce882a910c46c9c2013-06-27 10:26:36
OP_DUP OP_HASH160 77bd2ce6db4564c6a640fccd133da6371f89fd96 OP_EQUALVERIFY OP_CHECKSIG (1Bv82xxr341Uf7hZfWPy9RiNUkG4hx3CMf)
1184 Bestätigungen  0.4 BTC
Über uns & Kontakt: Über uns -  Status: Ok (566 Knoten verbunden) -  Erweitert: Aktivieren - Währung: