I am trying to build small app to generate Bitcoin Address (for sake of understanding).
I use OpenSSL library.
I managed to convert private key to public key, hashed public key with sha256, and result was fine. But, then, problem appears when I try to run sha256 result trough ripemd160.
- I tested ripemd160 function with plain string and it works fine
- I did converted sha256 result to string
- Still I get wrong result
Here's my main:
int _tmain(int argc, _TCHAR* argv[]){ char sha256_buffer[65]; char ripemd160_buffer[41]; char *pvt_key = "18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725"; unsigned char *pub_hex = priv2pub((const unsigned char *)pvt_key, POINT_CONVERSION_UNCOMPRESSED ); //printf("%s\n", pub_hex); std::string pub_key_string = hex_to_string(reinterpret_cast<char*>(pub_hex)); sha256(&pub_key_string[0], sha256_buffer); printf("%s\n", sha256_buffer); std::string hash256_string = hex_to_string(reinterpret_cast<char*>(sha256_buffer)); ripemd160(&hash256_string[0], ripemd160_buffer); printf("%s\n", ripemd160_buffer); return 0;}
Here's my ripemd160 function:
void ripemd160(char *string, char outputBuffer[41]){ unsigned char hash[RIPEMD160_DIGEST_LENGTH]; RIPEMD160_CTX ripemd160; RIPEMD160_Init(&ripemd160); RIPEMD160_Update(&ripemd160, string, strlen(string)); RIPEMD160_Final(hash, &ripemd160); for (int i = 0; i < RIPEMD160_DIGEST_LENGTH; i++) { sprintf_s(outputBuffer + (i * 2), sizeof(outputBuffer + (i * 2)), "%02x", hash[i]); } outputBuffer[40] = 0;}
Here's my hex to string function:
string hex_to_string(const string& in) { string output; if ((in.length() % 2) != 0) { throw runtime_error("String is not valid length ..."); } size_t cnt = in.length() / 2; for (size_t i = 0; cnt > i; ++i) { uint32_t s = 0; stringstream ss; ss << hex << in.substr(i * 2, 2); ss >> s; output.push_back(static_cast<unsigned char>(s)); } return output;}
I am using example from
https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
This is the Ripemd-160 I suppose to get:010966776006953D5567439E5E39F86A0D273BEE
This is the Ripemd-160 I am actually getting:6c9814cf2a93131c8d3263158896e786de7a3f21
Hi I am making a simple program using python2.7 in which the first input is hex (32bytes) that will be hashed and increment by 1. The new value will be hashed again and increment again. The process will repeat until it satisfy the specified range.
However I'm getting an error with int()
TypeError: int() can't convert non-string with explicit base
Below is my program code
from coinkit.address import Addressimport hashlibh = hashlib.new('ripemd160') # <-- Create the hasha = Address.from_secret('0000000000000000000000000000000000000000000000000000000000000001') #where the input will be hashfor i in range (0, 10): # should have 10 outputs intVal = int(a, 16) # convert to hex intVal += 1 # increment by 1 h.update(hex(intVal)) # <-- Update the hash with the new incremented integer a = Address.from_secret(h.hexdigest()) # <-- Get the digest and feed it back into from_secret print a.pub, a.priv # <-- print new 'a' values
I did try to remove 16 it throws an error:
TypeError : int() argument must be a string or a number, not 'Address'
Please enlighten me. Thank you.
everything i have tried has given me wrong output values. i even copied C codes and changed them so that they would work in python and i still get wrong outputs. what is wrong?
import os, mathdef makehex(value,size=8): value = hex(value)[2:] if value[-1] == 'L': value = value[0:-1] while len(value)<size: value = '0' + value return valuedef makebin(value,size=32): value = bin(value)[2:] while len(value)<size: value = '0' + value return valuedef ROL(value,n): return (value << n) | (value >> 32-n)def little_end(string,base = 16): t = '' if base == 2: s= 8 if base == 16: s = 2 for x in range(len(string)/s): t = string[s*x:s*(x+1)] + t return tdef F(x,y,z,round): if round<16: return x ^ y ^ z elif 16<=round<32: return (x & y) | (~x & z) elif 32<=round<48: return (x | ~y) ^ z elif 48<=round<64: return (x & z) | (y & ~z) elif 64<=round: return x ^ (y | ~z)def RIPEMD160(data):# constants h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE;h3 = 0x10325476; h4 = 0xC3D2E1F0 k = [0, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E] kk = [0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9,0] s = [ 11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8, 7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12, 11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5, 11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12, 9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6] ss= [ 8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6, 9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11, 9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5, 15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8, 8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11] r= range(16) + [ 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13] rr = [ 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11] # md4 padding + preprocessing temp = '' for x in data: temp += makebin(ord(x),8) length = len(temp)%2**64 temp +='1' while len(temp)%512!=448: temp+='0' input = temp temp = '' for x in range(len(input)/32): temp += little_end(input[32*x:32*(x+1)],2) input = temp temp = makebin(length,64) input += temp[32:]+temp[:32] t = len(input)/512 # the rounds for i in range(t): # i called the parallel round variables 2x the other round variable: a -> aa a = aa = h0; b = bb = h1; c = cc = h2; d = dd = h3; e = ee = h4 X = input[512*i:512*(i+1)] X = [int(X[32*x:32*(x+1)],2) for x in range(16)] for j in range(80): T = (a+ ROL( (F(b, c, d, j) + X[r[j]] + k[j/16])%2**32,s[j])+e)%2**32 c = ROL(c, 10) a = e; e = d; d = c; c = b; a = T T = (aa+ ROL( (F(bb,cc,dd,79-j) + X[rr[j]] + kk[j/16] )%2**32,ss[j])+ee)%2**32 cc = ROL(cc,10) aa = ee; ee = dd; dd = cc; cc = bb; aa = T T = (h1+c+dd)%2**32 h1 = (h2+d+ee)%2**32 h2 = (h3+e+aa)%2**32 h3 = (h4+a+bb)%2**32 h4 = (h0+b+cc)%2**32 h0 = T return little_end(makehex(h0))+little_end(makehex(h1))+little_end(makehex(h2))+little_end(makehex(h3))+little_end(makehex(h4)) data = RIPEMD160('') print data,data =='9c1185a5c5e9fc54612808977ee8f548b2258d31' # its always false
I am looking for another approach to apply RIPEMD-160 to the second column of a csv file.
Here is my code
awk -F "," -v env_var="$key" '{ tmp="echo -n \047" $2 env_var "\047 | openssl ripemd160 | cut -f2 -d\047 \047" if ( (tmp | getline cksum) > 0 ) { $3 = toupper(cksum) } close(tmp) print}' /test/source.csv > /ziel.csv
I run it in a big csv file (1Go), it takes 2 days and I get only 100Mo, that means i need to wait a month to get all my new CSV.
Can you help me with another idea and approach to get my data faster.
Thanks in advance
I am trying to understand how the crypto algorithms RIPEMD and SHA256 work. The bitcoin method for computing PKHash is RIPEMD160(SHA256(PublicKey)).
I am trying to first implement the RIPEMD of SHA256(PublicKey).
pkHashStep1=hashlib.sha256(public_key.decode('hex')).digest()print 'MyTransaction pkHashStep1 {}'.format(pkHashStep1)MyTransaction pkHashStep1 ▒▒▒so▒/▒▒e▒▒▒¡▒7▒?9▒▒.▒ӟ!n▒h
This outputs a string that I cannot use directly, but the hashlib library can use this. Trying pkHashStep1.decode('hex') and bin(pkHashStep1) throws an error. How does it convert the hash to a usable hexstring/bin??
Currently, I have publicKey as input to my RipeMD method, instead of the pkHashStep1, and have to separately do
input=hashlib.sha256(publicKey.decode('hex')).hexdigest()
FYI: I know there is a ripemd method in hashlib. Suggesting I use it is not an answerhttps://stackoverflow.com/a/2124289/4219479
Casa - Mapa do Site - Privacidade - Links - Copyright © 2018 Cortex IT Ltd : Contato : admin @ cortexit.co.uk
Please note that by viewing our site you agree to our use of cookies (see Privacidade for details). You will only see this message once.