简体   繁体   English

比特币哈希算法

[英]Bit Coin Hashing Algorithm

I, like many hopeless romantics in the online world, believe that I can successfully mine bit coins. 我像网络世界中许多绝望的恋人一样,相信我可以成功地开采比特币。 Problems is, I can't get the basic algorithm correct for taking data from a previous mine block and creating the hash for the next mined block. 问题是,我不能正确地获取从上一个矿区获取数据并为下一个开采区创建哈希的基本算法正确。 I understand from several articles that we start with the current hash of a block, reverse and append the merkel root, add the nonce, then get the SHA256 hash of that string. 我从几篇文章中了解到,我们从块的当前哈希开始,反向并添加默克尔根,添加随机数,然后获得该字符串的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();
    }

Anyone know the correct algorithm? 有人知道正确的算法吗? I used this block https://blockchain.info/block-height/477065 and tried to calculate the hash for the next block. 我使用了这个区块https://blockchain.info/block-height/477065并尝试计算下一个区块的哈希值。

So I was curious about the same thing. 所以我对同一件事感到好奇。 This is what I came up with. 这就是我想出的。 It's obviously not production quality, but it gets the idea across and seems to work. 显然这不是生产质量,但它可以使想法传播并似乎奏效。

It probably would have been much easier if it weren't for the reverse and swap logic that was practically undocumented everything I was looking. 如果不是因为反向和交换逻辑实际上没有记录我正在寻找的所有内容,那可能会容易得多。

Anyways, hope this helps. 无论如何,希望这会有所帮助。 I found a bunch of help here if you're interested: http://trogers.net/2018/01/29/how-to-validate-a-bitcoin-blocks-proof-of-work-c/ 如果您对此感兴趣,我在这里找到了很多帮助: 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM