In the previous post,
I walked through how to store a hash in the Bitcoin blockchain, by converting the hash to a valid Bitcoin address and sending a small amount of bitcoin to it.
I neglected to include a script that goes the other way, so here it is.
Below is a script that will take a valid Bitcoin address and convert it back to the hash it came from. This can be used to validate Blockchain Identities.
#!/usr/bin/env ruby# address-to-hash.rb# Copyright (C) 2015 Joe Ruether firstname.lastname@example.org## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.# Get the addressaddress=ARGVdefdecode_base58(base58_val)alpha="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"int_val,base=0,alpha.sizebase58_val.reverse.each_char.with_indexdo|char,index|raiseArgumentError,'Value not a valid Base58 String.'unlesschar_index=alpha.index(char)int_val+=char_index*(base**index)ends=int_val.to_s(16)s=(s.bytesize.odd??'0'+s:s)s=''ifs=='00'leading_zero_bytes=(base58_val.match(/^(+)/)?$1:'').sizes=("00"*leading_zero_bytes)+sifleading_zero_bytes>0send# Get the hex payloadpayload=decode_base58(address)# Print out the first 20 bytes of the sha256 hashputspayload[2..41]