SHA256算法的实现没有return预期的结果

Implementation of the SHA256 algorithm do not return the expected result

通过下面的实现,基于可用的伪代码 here,我试图转换一个由 class:

的成员串联生成的字符串
class BlockHeader
{
private:
  int version;
  string hashPrevBlock;
  string hashMerkleRoot;
  int time;
  int bits;
  int nonce;
}

进入 SHA256 哈希,就像下面的 python 代码所做的一样,可用 here:

>>> import hashlib
>>> header_hex = ("01000000" +
 "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
 "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
 "c7f5d74d" +
 "f2b9441a" +
 "42a14695")
>>> header_bin = header_hex.decode('hex')
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
>>> hash.encode('hex_codec')
'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000'
>>> hash[::-1].encode('hex_codec')
'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'

我希望我的程序 return 与上面的程序 returned 产生相同的结果,但是相反,当我编译并且 运行 时:

int main() {
  BlockHeader header;
  header.setVersion(0x01000000);
  header.setHashPrevBlock("81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000");
  header.setHashMerkleRoot("e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b");
  header.setTime(0xc7f5d74d);
  header.setBits(0xf2b9441a);
  header.setNonce(0x42a14695);

  Sha256 hash1(header.bytes());
  array<BYTE, SHA256_BLOCK_SIZE> h1 = hash1.hash();

  cout << "hash1: ";
  for(int i=0; i<h1.size(); i++)
    printf("%.2x", h1[i]);
  printf("\n");

  Sha256 hash2(h1);
  array<BYTE, SHA256_BLOCK_SIZE> h2 = hash2.hash();

  cout << "hash2: ";
  for(int i=0; i<h2.size(); i++)
    printf("%.2x", h2[i]);
  printf("\n");
}

结果是:

hash1: e2245204380a75c6bc6ac56f0000000040030901000000001100011000000000
hash2: 68a74f2a36c8906068c6cd6f00000000020000000000000080a7d06f00000000

我知道我的程序中的字节顺序与 python 结果不同,但是我可以稍后在得到正确的结果时解决这个问题。查看下面的代码,任何人都可以提示我在这里缺少什么吗?

#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))

#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))

#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))

#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))

Sha256::Sha256(vector<BYTE> data) {
    SIZE64 L = data.size() / 2;
    SIZE64 K = 0;
    while( (L + 1 + K + 8) % 64 != 0)
        K = K + 1;

    for(int i=0; i<L; i++) {
        BYTE c = (data[i] % 32 + 9) % 25 * 16 + (data[i+1] % 32 + 9) % 25;
        source.push_back(c);
    }

    source.push_back(0x80);

    for(int i=0; i<K; i++)
        source.push_back(0x00);

    SIZE64 x = L + 1 + K + 8;
    for(int i=0; i<sizeof(x); i++)
        source.push_back( x >> i*8 );
}

Sha256::Sha256(array<BYTE, SHA256_BLOCK_SIZE> data) {
    SIZE64 L = data.size() / 2;
    SIZE64 K = 0;
    while( (L + 1 + K + 8) % 64 != 0)
        K = K + 1;

    for(int i=0; i<L; i++) {
        BYTE c = (data[i] % 32 + 9) % 25 * 16 + (data[i+1] % 32 + 9) % 25;
        source.push_back(c);
    }

    source.push_back(0x80);

    for(int i=0; i<K; i++)
        source.push_back(0x00);

    SIZE64 x = L + 1 + K + 8;
    for(int i=0; i<sizeof(x); i++)
        source.push_back( x >> i*8 );
}

array<BYTE, SHA256_BLOCK_SIZE> Sha256::hash() {
    array<BYTE, SHA256_BLOCK_SIZE> result;

    WORD32 h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a, h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19;

    WORD32 k[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};

    WORD32 a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];

    for(int chunk=0; chunk<=source.size()/64; chunk++) {
        for (i = 0, j = chunk*64; i < 16; ++i, j += 4)
            m[i] = (source[j] << 24) | (source[j + 1] << 16) | (source[j + 2] << 8) | (source[j + 3]);
        for ( ; i < 64; ++i)
            m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];

        a = h0;
        b = h1;
        c = h2;
        d = h3;
        e = h4;
        f = h5;
        g = h6;
        h = h7;

        for (i = 0; i < 64; ++i) {
            t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
            t2 = EP0(a) + MAJ(a,b,c);
            h = g;
            g = f;
            f = e;
            e = d + t1;
            d = c;
            c = b;
            b = a;
            a = t1 + t2;
        }

        h0 += a;
        h1 += b;
        h2 += c;
        h3 += d;
        h4 += e;
        h5 += f;
        h6 += g;
        h7 += h;
    }

    for(int i=0; i<4; i++) result[0] = h0 >> i;
    for(int i=0; i<4; i++) result[1] = h1 >> i;
    for(int i=0; i<4; i++) result[2] = h2 >> i;
    for(int i=0; i<4; i++) result[3] = h3 >> i;
    for(int i=0; i<4; i++) result[4] = h4 >> i;
    for(int i=0; i<4; i++) result[5] = h5 >> i;
    for(int i=0; i<4; i++) result[6] = h6 >> i;
    for(int i=0; i<4; i++) result[7] = h7 >> i;

  return result;
}

Sha256::hash函数中,result是一个BYTE数组,而h0是一个WORD32。您可能想将 h0 拆分为 4 个 BYTE 并存储到 result 数组中,但是函数末尾的 for 循环无法实现您的目标。

你要做的是将h0连接到h7,然后通过移动24、16、8,从h0提取字节到h7, 0 位:

// concatenate h0 to h7
WORD32 hs[8] = {h0, h1, h2, h3, h4, h5, h6, h7};

// extract bytes from hs to result
for(int i=0; i<8; i++) { // loop from h0 to h7
    result[i*4  ] = hs[i] >> 24; // the most significant byte of h_i
    result[i*4+1] = hs[i] >> 16;
    result[i*4+2] = hs[i] >> 8;
    result[i*4+3] = hs[i];       // the least significant byte of h_i
}

编辑

经过一番测试,我又发现了一个错误:

for(int chunk=0; chunk<=source.size()/64; chunk++) {
                      ^^

应该是

for(int chunk=0; chunk<source.size()/64; chunk++) {
                      ^

chuck从0开始,所以应该用<代替<=.
例如,当 source.size() 为 64 时,您只有 1 个块要处理。

编辑2

我全面测试了您的代码,发现 Sha256 class.

的构造函数中存在两个问题

您的代码暗示您假定传递给构造函数的 vector<BYTE> 是一个 十六进制字符串 。没关系,但是您对 array<BYTE, SHA256_BLOCK_SIZE> 版本使用相同的代码,即 hash() 函数的 return 类型,return 是一个 BYTE 数组十六进制字符串。

对于 BYTE 数组,您可以简单地将字节 data[i] 推入 source。另外,L 应该是 data.size() 因为每个元素在字节数组中的大小都是 1。

此外,您尝试将输入的大小 (x) 附加到 source,但 x 不应包括附加的 1 和 0,它是 bit 输入的计数,所以 x 应该只是 L*8。另外,大小应该是一个big-endian整数,所以你必须先压入更大的字节:

for(int i=0; i<sizeof(x); i++) // WRONG: little endian
for(int i=sizeof(SIZE64)-1; i>=0; i--) // Correct: big endian

我已经让它正确执行并输出:

hash1: b9d751533593ac10cdfb7b8e03cad8babc67d8eaeac0a3699b82857dacac9390
hash2: 1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000

如果遇到其他问题,欢迎随时提问。你非常接近正确答案。希望你能成功修复所有错误:)

EDIT3:其他功能的实现

struct BlockHeader {
    int version;
    string hashPrevBlock;
    string hashMerkleRoot;
    int time;
    int bits;
    int nonce;
    vector<BYTE> bytes();
};

#define c2x(x) (x>='A' && x<='F' ? (x-'A'+10) : x>='a' && x<='f' ? (x-'a'+10) : x-'0')
vector<BYTE> BlockHeader::bytes() {
    vector<BYTE> bytes;
    for (int i=24; i>=0; i-=8) bytes.push_back(version>>i);
    for (int i=0; i<hashPrevBlock.size(); i+=2)
        bytes.push_back(c2x(hashPrevBlock[i])<<4 | c2x(hashPrevBlock[i+1]));
    for (int i=0; i<hashMerkleRoot.size(); i+=2)
        bytes.push_back(c2x(hashMerkleRoot[i])<<4 | c2x(hashMerkleRoot[i+1]));
    for (int i=24; i>=0; i-=8) bytes.push_back(time>>i);
    for (int i=24; i>=0; i-=8) bytes.push_back(bits>>i);
    for (int i=24; i>=0; i-=8) bytes.push_back(nonce>>i);
    return bytes; // return bytes instead of hex string
}
// exactly the same as the vector<BYTE> version
Sha256::Sha256(array<BYTE, SHA256_BLOCK_SIZE> data) {
    SIZE64 L = data.size(); // <<
    SIZE64 K = 0;
    while( (L + 1 + K + 8) % 64 != 0)
        K = K + 1;
    // can be simplified to: int K = (128-1-8-L%64)%64;

    // ** thanks to "chux - Reinstate Monica" pointing out i should be a SIZE64
    for(SIZE64 i=0; i<L; i++) { // **
        source.push_back(data[i]); // <<
    }

    source.push_back(0x80);

    for(int i=0; i<K; i++)
        source.push_back(0x00);

    SIZE64 x = L*8; // <<
    for(int i=sizeof(SIZE64)-1; i>=0; i--) { // big-endian
        source.push_back(x >> i*8);
    }
}

EDIT4:for 循环中的可变大小

正如“chux - Reinstate Monica”所指出的,如果数据的大小大于 INT_MAX,则可能会出现问题。所有使用大小作为上限的 for-loop 都应该使用 size_t 类型的计数器(而不是 int)来防止这个问题。

// in BlockHeader::bytes()
for (size_t i=0; i<hashPrevBlock.size(); i+=2)
// in Sha256::hash()
for (size_t chunk=0; chunk<source.size()/64; chunk++)
// in main()
for (size_t i=0; i<h1.size(); i++)
for (size_t i=0; i<h2.size(); i++)

请注意 size_tunsigned。反向版本不起作用,因为 i 永远不会小于 0。

for (size_t i=data.size()-1; i>=0; i--) // infinite loop