Bitcoin Forum
October 19, 2017, 04:41:10 PM *
News: Latest stable version of Bitcoin Core: 0.15.0.1  [Torrent]. (New!)
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: How do I perform the SHA-256 hashing on a public key?  (Read 996 times)
sjors
Newbie
*
Offline Offline

Activity: 11


View Profile
May 09, 2013, 11:47:30 PM
 #1

I'm trying to understand bitcoin better by trying things out in Ruby. I know some aspects of Bitcoin have already been implemented in Ruby, but I'm trying to avoid "cheating" by studying those.

I read the wiki regarding scripts as well as 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 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...
1508431270
Hero Member
*
Offline Offline

Posts: 1508431270

View Profile Personal Message (Offline)

Ignore
1508431270
Reply with quote  #2

1508431270
Report to moderator
1508431270
Hero Member
*
Offline Offline

Posts: 1508431270

View Profile Personal Message (Offline)

Ignore
1508431270
Reply with quote  #2

1508431270
Report to moderator
Visit and contribute to reddit.com/r/Bitcoin
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
sjors
Newbie
*
Offline Offline

Activity: 11


View Profile
May 10, 2013, 10:10:21 AM
 #2

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.
Vergil77
Newbie
*
Offline Offline

Activity: 1



View Profile
July 04, 2013, 02:01:11 PM
 #3

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:
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!