gpt4 book ai didi

c# - 比特币哈希算法

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:53 25 4
gpt4 key购买 nike

我和网络世界中许多无可救药的浪漫主义者一样,相信我可以成功开采比特币。问题是,我无法获得正确的基本算法来从之前的矿 block 中获取数据并为下一个矿 block 创建哈希。我从几篇文章中了解到,我们从 block 的当前哈希开始,反转并追加默克尔根,添加随机数,然后获取该字符串的 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();
}

有人知道正确的算法吗?我用了这个 block https://blockchain.info/block-height/477065并尝试计算下一个区 block 的哈希值。

最佳答案

所以我对同样的事情很好奇。这就是我想出的。这显然不是生产质量,但它传达了这个想法并且似乎奏效了。

如果不是因为反向和交换逻辑几乎没有记录我正在寻找的一切,它可能会容易得多。

无论如何,希望这对您有所帮助。如果您有兴趣,我在这里找到了很多帮助:http://trogers.net/2018/01/29/how-to-validate-a-bitcoin-blocks-proof-of-work-c/

class Program
{
static void Main(string[] args)
{
// https://blockchain.info/block-height/286819
int version = 2;
string previousBlock = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717";
string merkelRoot = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a";
uint nonce = 856192328;
long timestamp = new DateTimeOffset(DateTime.SpecifyKind(DateTime.Parse("2014-02-20 04:57:25"), DateTimeKind.Utc)).ToUnixTimeSeconds();
uint bits = 419520339;

var header = new StringBuilder()
.Append(ReverseAndSwap(version.ToString("D8")))
.Append(ReverseAndSwap(previousBlock))
.Append(ReverseAndSwap(merkelRoot))
.Append(ReverseAndSwap(timestamp.ToString("x2")))
.Append(ReverseAndSwap(bits.ToString("x2")))
.Append(ReverseAndSwap(nonce.ToString("x2")))
.ToString();

Debug.Assert(string.CompareOrdinal(header, "0200000017975b97c18ed1f7e255adf297599b55330edab87803c81701000000000000008a97295a2747b4f1a0b3948df3990344c0e19fa6b2b92b3a19c8e6badc141787358b0553535f011948750833") == 0);

var bytes = HexToBytes(header);

SHA256 sha = new SHA256Managed();

bytes = sha.ComputeHash(sha.ComputeHash(bytes)).Reverse().ToArray();

var hash = BytesToHex(bytes);

Debug.Assert(string.CompareOrdinal(hash, "0000000000000000e067a478024addfecdc93628978aa52d91fabd4292982a50") == 0);
}

private static string ReverseAndSwap(string input)
{
StringBuilder sb = new StringBuilder();

for (var i = input.Length - 1; i >= 0; i--)
{
sb.Append(input[i - (i % 2 == 0 ? -1 : 1)]);
}

return sb.ToString();
}

public static byte[] HexToBytes(string hex)
{
byte[] hexAsBytes = new byte[hex.Length / 2];

for (int index = 0; index < hexAsBytes.Length; index++)
{
string byteValue = hex.Substring(index * 2, 2);
hexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}

return hexAsBytes;
}

public static string BytesToHex(byte[] bytes)
{
var output = new StringBuilder(bytes.Length * 2);

for (int i = 0; i < bytes.Length; ++i)
{
output.AppendFormat("{0:x2}", bytes[i]);
}

return output.ToString();
}
}

关于c# - 比特币哈希算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com