如何计算结构体的哈希值

3

我正在寻找计算数据结构哈希值的解决方案。假设我们有这样一个结构:

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(const char * input, uint8_t (&output)[32]),它的参数是输入和输出-计算出来的哈希。我知道我需要将结构中的每个值转换为const char *。但我的问题是下一步该怎么做,我应该为数组中的每个值、x和y计算一个单独的哈希,然后将它们相加吗?

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


2
如果您想为一个结构计算哈希值,那么哈希值不应该在该结构中。 - Eugene Sh.
1
你可能无法使用你的Sha256函数完成这个任务。该函数可能是为以空字符结尾的字符串作为输入而设计的。你需要告诉我们更多关于该函数的信息。 - Jabberwocky
2
你如何告诉Sha256函数输入数组中有多少字节? - selbie
1
你想让哈希做什么?你想防止意外或恶意修改吗? - Alan Birtles
1
如果您需要验证接收到的哈希值但不知道接收到的哈希值是如何计算的,那么您就无法做到。没有通用的、不可侵犯的计算哈希值的方法,也没有将多个值组合在哈希过程中的方法。请向发送哈希值的实体询问如何计算它。 - JaMiT
显示剩余6条评论
1个回答

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);
}

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


问题在于我无法在我的项目中使用boost库。 - Mateusz Czerepowicki
你可以使用什么?任何第三方库吗? - parktomatomi
不,我只能使用c++03并遵守Misra规则。 - Mateusz Czerepowicki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接