您的位置:首页 > 其它

椭圆曲线公钥转换为比特币地址原理

2016-10-21 20:21 330 查看
Assuming what you're doing is based on this conversion:



I'll describe what you can do in pseudo-code:

First extract x, y from public key.
// get x and y from Public key "point"
EC_POINT_get_affine_coordinates_GFp(group, pub_key, x, y, ctx);
// convert BIGNUMs x, y into binary form
BN_bn2bin(x,x_char);
BN_bn2bin(y,y_char);


Next you need to do message digest several times, including 3 sha256 and 1 ripemd160. In the following pseudo-code I'll show you how to do ripemd160. To do sha256 with EVP_MD, just replace 
EVP_ripemd160()
 with 
EVP_sha256()
,
and update (input to EVP_MD) your input message with single or several 
EVP_DigestUpdate()
.
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&md_ctx);
EVP_DigestInit(&md_ctx, EVP_ripemd160());
// hdr = 0x04
EVP_DigestUpdate(&md_ctx,hdr,1);
EVP_DigestUpdate(&md_ctx,x_char,32);
EVP_DigestUpdate(&md_ctx,y_char,32);
// put message degest into dgst and set length to dgstlen
EVP_DigestFinal(&md_ctx,dgst,&dgstlen);
EVP_MD_CTX_cleanup(&md_ctx);


Or the easier way, call 
sha256()
 and 
ripemd160()
 directly.
But you need to prepare your input message before calling hash functions 
sha256()
 or 
ripemd160()
.

The 25-byte binary address is the result of ripemd160, together with the first 4 bytes of 32-byte checksum. You need to find a way to convert it from Base 256 to Base 58. I don't think OpenSSL support that.

shareimprove
this answer
answered Jul 17 '13 at 2:56





ChiaraHsieh
2,0241230

 
Assuming what you're doing is based on this conversion:



I'll describe what you can do in pseudo-code:

First extract x, y from public key.
// get x and y from Public key "point"
EC_POINT_get_affine_coordinates_GFp(group, pub_key, x, y, ctx);
// convert BIGNUMs x, y into binary form
BN_bn2bin(x,x_char);
BN_bn2bin(y,y_char);


Next you need to do message digest several times, including 3 sha256 and 1 ripemd160. In the following pseudo-code I'll show you how to do ripemd160. To do sha256 with EVP_MD, just replace 
EVP_ripemd160()
 with 
EVP_sha256()
,
and update (input to EVP_MD) your input message with single or several 
EVP_DigestUpdate()
.
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&md_ctx);
EVP_DigestInit(&md_ctx, EVP_ripemd160());
// hdr = 0x04
EVP_DigestUpdate(&md_ctx,hdr,1);
EVP_DigestUpdate(&md_ctx,x_char,32);
EVP_DigestUpdate(&md_ctx,y_char,32);
// put message degest into dgst and set length to dgstlen
EVP_DigestFinal(&md_ctx,dgst,&dgstlen);
EVP_MD_CTX_cleanup(&md_ctx);


Or the easier way, call 
sha256()
 and 
ripemd160()
 directly.
But you need to prepare your input message before calling hash functions 
sha256()
 or 
ripemd160()
.

The 25-byte binary address is the result of ripemd160, together with the first 4 bytes of 32-byte checksum. You need to find a way to convert it from Base 256 to Base 58. I don't think OpenSSL support that.

shareimprove
this answer
answered Jul 17 '13 at 2:56





ChiaraHsieh
2,0241230

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: