【发布时间】:2018-04-12 03:00:06
【问题描述】:
我和网络世界中许多绝望的浪漫主义者一样,相信我可以成功地挖到比特币。问题是,我无法获得正确的基本算法来从前一个矿块获取数据并为下一个矿块创建哈希。我从几篇文章中了解到,我们从区块的当前哈希开始,反向并附加默克尔根,添加随机数,然后获取该字符串的 SHA256 哈希。
static void Main(string[] args)
{
//get the hash of the current block
string currentHash =
@"000000000000000000c5c04011f9a3fb5f46064fed7e06dcdae69024ed6484c1";
//get the merkel root
string merkel =
@"f73a382814c51cbc5a59ab9817ac54c63decb7b3dac5b049df5213c029162bdf";
//reverese the merkel root
char[] c = merkel.ToCharArray();
Array.Reverse(c);
merkel = new string(c);
//get a hash object that returns SHA256
Hash hash = new Hash();
//get the nonce that mined the block
uint nonce = 3546041956;
//string together current hash, merkel root and the hex of the nonce
string stringTotal = currentHash + merkel + nonce.ToString("x2");
//calculate the SHA256 hash of the
string nextHash = hash.GetHash(stringTotal);
Console.WriteLine(nextHash);
Console.ReadKey();
}
有人知道正确的算法吗?我使用这个块https://blockchain.info/block-height/477065 并尝试计算下一个块的哈希。
【问题讨论】:
-
散列只在一堆字节上完成。在谈论散列时您会看到的任何字符串都只是字节的表示。
-
Hash 对象接收字符串,转换为字节,应用 SHA256 算法,然后将 byte[] 中的每个字节附加到带有 ("x2") 的 StringBuilder 对象。
-
public string GetHash(string input) { string hash = ""; byte[] b = Encoding.UTF8.GetBytes(input); HashAlgorithm ha = new SHA256CryptoServiceProvider();字节[] b2 = ha.ComputeHash(b); StringBuilder builder = new StringBuilder(); foreach(b2 中的字节 i) { builder.Append(i.ToString("x2")); } 哈希 = builder.ToString();返回哈希; }
-
反转默克尔根 - 我不敢相信你必须反转字节表示的字符(2个字符代表1个字节)
-
将十六进制字符串表示解码为字节数组,然后反转该字节数组。散列时远离字符串,只使用流或字节数组。