如何计算结构的哈希

时间:2020-01-10 17:10:10

标签: c++ hash sha256 sha

我正在寻找解决方案,该方法如何为数据结构计算哈希。假设我们具有这样的结构:

struct A
{
    float64_t array[4][4];
    float64_t x;
    float64_t y;
    uint8_t validationHash[32]; // here computed hash need to be stored
}

我还具有函数Sha256(cont char * input, uint8_t (&output)[32]),该函数作为参数接受输入和输出-计算得出的哈希值。我知道我需要将每个值从结构转换为const char *。但是我的问题是下一步该怎么做,我应该为数组x和y中的每个值分别计算一个哈希值,然后将它们加在一起还是什么?

Sha256的实现与此处http://www.zedwood.com/article/cpp-sha256-function

相同

1 个答案:

答案 0 :(得分:2)

您链接到的SHA-256哈希函数(与大多数加密哈希实现一样)接受字节数组作为其输入。因此,第一步是序列化要散列的数据。

这不像将结构强制转换为字节数组那样简单。序列化应该在操作系统和硬件之间可移植。系统之间结构的对齐方式,字节序等可能会有所不同,因此最好使用序列化库,并将所有棘手的严格别名问题留给库作者。

最佳选择:序列化库

由于您似乎已经在使用Boost(float64_t类型),因此可以使用Boost序列化库。首先,创建一个序列化函数来指示Boost如何序列化A

namespace boost {
namespace serialization {

template<class Archive>
void serialize(Archive & ar, A & a, const unsigned int version)
{
    ar & a.array;
    ar & a.x;
    ar & a.y;
}

} // namespace serialization
} // namespace boost

然后,将其序列化为内存流:

std::ostringstream plaintext_buffer {};
{
    boost::archive::binary_oarchive oa(plaintext_buffer);
    oa << a;
}
std::string plaintext = plaintext_buffer.str();

现在您可以使用SHA-256哈希函数。我将把这一部分留给您练习。

  • 输入:plaintext.data()表示数据,plaintext.size()表示大小
  • 输出:a.validationHash

好的选择:自定义浮点序列化器

根据注释,您仅限于C ++ 03(我将其视为C ++ 98),并且不能使用任何库。因此,首先,让我们使用最接近的等效标准类型重新定义您的函数:

struct A
{
    double array[4][4];
    double x;
    double y;
    uint8_t validationHash[32]; // here computed hash need to be stored
}

我稍微修改了这个答案:Serialize double and float with C,它声称是便携式IEEE 754串行器。凉!我将输出更改为内存缓冲区,替换为goto,并将C类型转换转换为static_cast

void serializeIeee754(double x, uint8_t* destination)
{
    int                     shift;
    unsigned long           sign, exp, hibits, hilong, lowlong;
    double                  fnorm, significand;
    int                     expbits = 11;
    int                     significandbits = 52;

    if(x == 0) {
        /* zero (can't handle signed zero) */
        hilong = 0;
        lowlong = 0;
    } else if(x > DBL_MAX) {
        /* infinity */
        hilong = 1024 + ((1 << (expbits - 1)) - 1);
        hilong <<= (31 - expbits);
        lowlong = 0;
    } else if(x < -DBL_MAX) {
        /* -infinity */
        hilong = 1024 + ((1 << (expbits - 1)) - 1);
        hilong <<= (31 - expbits);
        hilong |= (1 << 31);
        lowlong = 0;
    } else if(x != x) {
        /* NaN - dodgy because many compilers optimise out this test
        * isnan() is C99, POSIX.1 only, use it if you will.
        */
        hilong = 1024 + ((1 << (expbits - 1)) - 1);
        hilong <<= (31 - expbits);
        lowlong = 1234;
    } else {
        /* get the sign */
        if(x < 0) {
            sign = 1;
            fnorm = -x;
        } else {
            sign = 0;
            fnorm = x;
        }

        /* get the normalized form of f and track the exponent */
        shift = 0;
        while(fnorm >= 2.0) {
            fnorm /= 2.0;
            shift++;
        }
        while(fnorm < 1.0) {
            fnorm *= 2.0;
            shift--;
        }

        /* check for denormalized numbers */
        if(shift < -1022) {
            while(shift < -1022) {
                fnorm /= 2.0;
                shift++;
            }
            shift = -1023;
        } else {
            /* take the significant bit off mantissa */
            fnorm = fnorm - 1.0;
        }
        /* calculate the integer form of the significand */
        /* hold it in a  double for now */

        significand = fnorm * ((1LL << significandbits) + 0.5f);

        /* get the biased exponent */
        exp = shift + ((1 << (expbits - 1)) - 1);   /* shift + bias */

        /* put the data into two longs */
        hibits = static_cast<long>(significand / 4294967296);  /* 0x100000000 */
        hilong = (sign << 31) | (exp << (31 - expbits)) | hibits;
        lowlong = static_cast<unsigned long>(significand - hibits * 4294967296);
    }

    destination[0] = lowlong & 0xFF;
    destination[1] = (lowlong >> 8) & 0xFF;
    destination[2] = (lowlong >> 16) & 0xFF;
    destination[3] = (lowlong >> 24) & 0xFF;
    destination[4] = hilong & 0xFF;
    destination[5] = (hilong >> 8) & 0xFF;
    destination[6] = (hilong >> 16) & 0xFF;
    destination[7] = (hilong >> 24) & 0xFF;
}

现在,您可以为A编写自己的序列化器,该序列化器将写入144字节缓冲区:

void serializeA(A& a, uint8_t destination[144]) {
    uint8_t* out = destination;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            serializeIeee754(a.array[i][j], out);
            out += 8;
        }
    }
    serializeIeee754(a.x, out);
    out += 8;
    serializeIeee754(a.y, out);
}

然后将该缓冲区提供给您的哈希函数。