讨论帖:比特币中的SHA256算法的实现与标准的SHA256算法实现是否相同?
近日閱讀了比特幣源碼中與哈希相關(guān)的部分,對(duì)于其中一些細(xì)節(jié)還是有不清晰的地方。
于是我寫(xiě)了一個(gè)小的測(cè)試demo:sha256_test,代碼下載
分別測(cè)試了三個(gè)版本對(duì)于SHA-256算法的實(shí)現(xiàn):
- Bitcoin Version:來(lái)自比特幣核心源碼中對(duì)于SHA-256的實(shí)現(xiàn)
- crypto Version: 來(lái)自于Github上開(kāi)源算法庫(kù)crypto-algorithms中的標(biāo)準(zhǔn)SHA-256實(shí)現(xiàn)
- Tool Version: 來(lái)自于在線測(cè)試工具的SHA-256計(jì)算
對(duì)比發(fā)現(xiàn),開(kāi)源算法庫(kù)crypto-algorithms與在線SHA-256測(cè)試工具對(duì)于同一數(shù)據(jù)的哈希結(jié)果相同,但 Bitcoin Version 的哈希結(jié)果與其余兩個(gè)測(cè)試版本不一致。因此,我想了解:
比特幣中的SHA256算法的實(shí)現(xiàn)與標(biāo)準(zhǔn)的SHA256算法實(shí)現(xiàn)是否相同?
如果相同,那一定是我對(duì)比特幣源碼的使用出現(xiàn)了問(wèn)題,可否指出我在測(cè)試時(shí)調(diào)用錯(cuò)誤的地方?
如果不相同,可否指出哪些實(shí)現(xiàn)細(xì)節(jié)有差異,比如或許大小端的差異?字符串轉(zhuǎn)換存在差異?
具體地,我將Bitcoin Core源碼中與SHA256相關(guān)的部分剝離出來(lái),放置于sha256_test目錄下,構(gòu)成Bitcoin Version的測(cè)試代碼
我將開(kāi)源算法庫(kù)crypto-algorithms中與SHA256相關(guān)的部分,放置于standard_implement_test目錄下,構(gòu)成crypto Version的測(cè)試代碼
在線測(cè)試工具對(duì)于字符串”hello”的計(jì)算結(jié)果如下:
Conversion Completed Your hash has been successfully generated. hex: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 HEX: 2CF24DBA5FB0A30E26E83B2AC5B9E29E1B161E5C1FA7425E73043362938B9824算法庫(kù)crypto-algorithms對(duì)于字符串”hello”的計(jì)算結(jié)果如下:
$ ./test the string to test SHA-256: helloresult of SHA-256 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824Bitcoin Version對(duì)于字符串”hello”的計(jì)算結(jié)果如下:
其中,我使用了兩種方法從哈希結(jié)果中解析出16進(jìn)制字符串,但均與希望的結(jié)果不一致。
Bitcoin Version包含了以下文件:
$ ls common.h sha256.h tinyformat.h utilstrencodings.cpp makefile standard_implementation uint256.cpp utilstrencodings.h sha256.cpp test.cpp uint256.htest.cpp的代碼如下:
#include<iostream>#include "sha256.h" #include "uint256.h"//using namespace std; int main(int argc, char* argv[]) {if(argc!=2){std::cout<<"please type a string to test sha-256"<<std::endl;return 1;}char* str = argv[1];std::cout << "The string to test SHA-256:" << std::endl;int strLen = strlen(str);for(char* p = str; p<str+strLen; p++){std::cout<<*p;}std::cout<<std::endl;uint256 sha256_result;// use code in Bitcoin Core to solve SHA-256CSHA256 sha;sha.Write((unsigned char*)str, strLen);sha.Finalize((unsigned char*)&sha256_result);// show hash result by GetHex()std::cout << std::endl << "Bitcoin SHA-256 result by GetHex():" << std::endl;std::cout << sha256_result.GetHex() << std::endl;// show hash result by parsing member variable data[Width]std::cout << std::endl << "Bitcoin SHA-256 result by parse data[Width]:" << std::endl;unsigned int size = sha256_result.size();unsigned char* pointer = sha256_result.begin();unsigned char temp;for(unsigned char* pdata = pointer; pdata < pointer + size; pdata++){temp = *pdata;unsigned char hex1 = temp >> 4;unsigned char hex2 = temp & 0x0f;if(hex1<10)std::printf("%c",hex1+48);elsestd::printf("%c",hex1+87);if(hex2<10)std::printf("%c",hex2+48);elsestd::printf("%c",hex2+87);}std::cout << std::endl; }如果哪位大神有見(jiàn)解,多多交流,還望賜教~
后續(xù)如果對(duì)于這個(gè)問(wèn)題有了答案,我會(huì)回來(lái)更新的。
總結(jié)
以上是生活随笔為你收集整理的讨论帖:比特币中的SHA256算法的实现与标准的SHA256算法实现是否相同?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql ==null_mysql
- 下一篇: FreeRTOS学习及移植笔记之二:在I