Created
January 12, 2018 05:38
-
-
Save philmb3487/27420532e1ae578fff4c4908676328c0 to your computer and use it in GitHub Desktop.
Computing Merkle Tree: Example in C++.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QDebug> | |
#include <QByteArray> | |
#include <QCryptographicHash> | |
QByteArray sha256(QByteArray in) | |
{ | |
QCryptographicHash hash (QCryptographicHash::Sha256); | |
hash.addData(in); | |
QByteArray res1 = hash.result(); | |
return res1; | |
} | |
QByteArray sha256d(QByteArray in) | |
{ | |
QCryptographicHash hash (QCryptographicHash::Sha256); | |
hash.addData(in); | |
QByteArray res1 = hash.result(); | |
hash.reset(); | |
hash.addData(res1); | |
return hash.result(); | |
} | |
QByteArray reverse_hash (QByteArray in) | |
{ | |
QByteArray res; | |
if (in.length() != 32) return QByteArray::fromHex(""); | |
for (int i = 0; i < 32; ++i) | |
{ | |
res.append(in[31 - i]); | |
} | |
return res; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication app(argc, argv); | |
{//BTC block 70003 | |
QByteArray a = QByteArray::fromHex("6a7df1c030c1ad32cbaa21aa491194ac3b945f0dbe61dff2ef95f9c62f5650ee"); | |
QByteArray b = QByteArray::fromHex("8e32e4a5ba602390a1e9cc019bd3a49a68e2116a56310e12c5bbeab142a67720"); | |
a = reverse_hash(a); | |
b = reverse_hash(b); | |
QByteArray c = a + b; | |
QByteArray m = sha256d( c ); | |
m = reverse_hash(m); | |
qDebug() << m.toHex(); | |
} | |
{//VTC block 800019 | |
QByteArray a = QByteArray::fromHex("32e60eb21632597c1c1545c63fae57df8d4455648b7d97150b7dcee706749588"); | |
QByteArray b = QByteArray::fromHex("1deed158ada568b9b193473095c8eb2cef2cffe60ccce15c3c82f599a09767a8"); | |
a = reverse_hash(a); | |
b = reverse_hash(b); | |
qDebug() << a.toHex(); | |
qDebug() << b.toHex(); | |
QByteArray c = a + b; | |
QByteArray m = sha256d( c ); | |
m = reverse_hash(m); | |
qDebug() << m.toHex(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment