之前学习了 Base58编解码 以及Base58Check
总结一下之前的学习成果,那就是
- Bash58编码把大整数,转换成按58进制表示的字符串
去掉了0 , O , I, l 以及 +, / 易混淆字符 - Base58Check在Base58基础上加上了版本信息以及校验
其实学习这些都是为了学习了了解STEEM以及比特币的地址啥的
今天我们来学习比特币的私钥
私钥即保存金钱的地方
之前我一直没搞懂私钥是啥玩意,听着很高大上有木有
经过一番深入的学习和研究,我终于得出一个惊天地泣鬼神的结论
那就是: 私钥就是保存金钱的地方
注意,不是钱包,是私钥。
为啥这么说呢?
因为即便的钱包保管的很好,只要你的比特币私钥泄露出去,那么你的钱,将不会属于你了。
钱包还在,钱没了,最悲惨的莫过于此吧?
通过进一步的学习,我又有重大发现了
私钥相当于你藏钱的地方,而地址是你收钱的入口
任何人都可以往地址这个入口里扔钱,但是没法往出掏钱
只有拥有私钥的人才可以。
再进一步学习,发现地址是由私钥产生的,而通过地址却无法计算得出私钥
就是说私钥到地址的过程是单向不可逆的。
私钥就是一个256位数字
说了半天,私钥到底是啥,其实私钥就是一个256位,取值处于1到n - 1之间的随机数
其中: n = 1.158 * 1077
搞了半天就是一个256位数字嘛。
据说sha256,就能轻易生成个256位随机数,那么是不是就可以用来产生私钥呢?
看了一下《Mastering Bitcoin》,果然可以
大致意思就是把随机源收集的比特串送给sha256来生成256位数,并判断是否处于1到n - 1之间。
让我用Hello World做比特串来生成一个试试
>>> import hashlib
>>> from binascii import hexlify, unhexlify
>>> s = hashlib.sha256(bytes('Hello World', 'utf-8')).digest()
>>> print(hexlify(s).decode('ascii'))
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
哈哈哈哈哈,我们也有私钥啦。
这个是真的私钥哦,可以导入钱包的哦
导入试试看,耶,咱也有比特币地址啦
去blockchain.info看了一下,居然,竟然真的有四笔交易
向伟大的程序员致敬!向Hello World致敬!
https://blockchain.info/address/1LGWpj3pqbzYWvTLEcBKP6CS5PQkxBgjXj
私钥的表示方法
然后我们在回头来看blockchain.info里的钱包
我哭了,无论选择哪种方式,都不是我导入的私钥呀,我的私钥哪里去了?
于是又仔细学习了一下,原来除了直接用字符串形式的数字
私钥还可以有不同的表示方式!
比如说上边两个图列出的
- WIF
- Base58
咦,知道我为啥学习Base58了吧!
Base58
先来看看Base58,Base58就是把生成的私钥串使用Base58编码
引入我们上一篇文章学的Base58编码
对a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
进行编码
结果如下:
和上边Base58编码完全一样有木有?
WIF
再来看WIF
WIF亦即: Wallet Import Format (WIF)
再次强烈推荐《Mastering Bitcoin》,我想要的它都讲
其中HEX
应该就是a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
WIF看图就是加上前缀加上校验码嘛,然后用Base58Check编码
知道为啥学Base58Check了吧?
试了一下生成的东西为:
而BlockChain的钱包里的开头是L, 很明显我这个是未经压缩的
咋压缩呢?看图是加了个0x01后缀,然后再编码,让我试试试看
完全一样有木有!
话说, Blockchain.info不厚道,明明是WIF-compressed,你非得显示的个WIF,这不是误导我呢吗?差评!
总结
- 私钥就是一个256位随机数(处于1到n - 1之间)
- 私钥可以用hashlib.sha256来生成
- 私钥可以表示为64位16进制串、Base58、WIF、WIF-compressed
- 64位16进制串就是把私钥直接转换
- Base58编码就是对64位16进制串直接编码
- WIF就是在64位16进制串对应的字节串前加上前缀0x80, 并用Base58Check编码
- WIF-compressed就是在64位16进制串对应的字节串前加上前缀0x80,并加上后缀0x01, 并用Base58Check编码
好了,今天就学习到这里。
免责声明,本文为个人理解,示例仅供参考
请勿使用文章中写到的地址,如果导致bitcoin损失,概不负责!
@oflyhigh nice exposition,will really like to gain more knowledge on python programming,block-chain technology is built on a miralds of programming language,whats your take on this?.you have been,followed,upvoted and resteem.kindly do same.... @detycoon
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
steemmerch.com
Check the steemmerch.com store it the number store for Everything Steemit.
link
http://steemmerch.com?rfsn=679429.0b3f8
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
zZZZ
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
你这个钱包地址估计别人也能控制的,这个私钥相当于已经公开了。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
不懂。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Wow. Many make small gifts for @oflyhigh
And I make a small gift MEW
I really like your work!
Especially the pict I'm interested to see the making of images that's so refreshing!
Resteem =)
https://steemit.com/https://steemit.com/introduceyourself/@pinkbiru21/meetball-delecious-but-die/@pinkbiru21/meetball-delecious-but-die
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
nice pos,,,,
please follow me,,, im follow back
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
長知識
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
https://steemit.com/story/@greatness96/finding-mr-perfect-husband-episode-20
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
谢谢分享。 我曾用ruby 也写过类似的东西
https://github.com/tensaix2j/rbtcrypto
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Though I would have love to make a comment on this post but the language is not something I understand lollll
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
涨姿势
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
加密學的確是很有趣,我有空也要惡補一下,先留個名
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
这个 “Hello World” 就很重要了, 被人猜出来也就丢了吧 ?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
转两个试试
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
地址是由私钥产生的,只进不出,可以导入钱包 , 私钥忘了或者被人计算出来,是不是就丢了币
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
很想搞懂,但資質有限。看不懂的人要怎麼辦呀?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
What is that ?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
i didn't understand anythings :(
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
听说私钥就像大海里的沙子一样多,用不完是吗?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
O大,可以考慮把這一系列的文章集成一個課程或者成書。可以教導很多人的。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
我这其实就是个学习过程,好多写的灰常好的书,比如我文中推荐的
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
通常例子里面的私钥都会有人试一试的,万一有呢。哈哈
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
哈哈!雖然聽過私鑰,但不知道竟然是256位數字!感謝分享>v<
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This post made my day !!!
Thanks for sharing it !!!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
https://steemit.com/bitcoin/@chenfol/and-meanwhile-the-bitcoin
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
会不会有这种情况,两个人的私钥相同? 因为都是随机生成的。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit