首页 > 解决方案 > 带有 K 个前导零的 SHA1 校验和

问题描述

我试图找到一种超级有效的方法来为下面的等式生成一个随机字符串:

checkSum = SHA1(fixed_string + random_string)

校验和必须有 k 前导零才能满足最终条件。当前的实现是使用结合随机字符串生成器的蛮力方法。但是如果K大于7,这种方法效率低且耗时。

static void
gen_random(char *s) {
    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    for (size_t i = 0; i < LENGTH - 1; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }
}

有没有人想开发一种有效的算法来生成随机字符串以有效地处理条件?另一个问题是关于(fixed_string + random_string)的长度。正确处理 SHA1 校验和的最佳长度是多少?

谢谢

标签: c++cc++11sha1cryptocurrency

解决方案


If I understand correctly, you trying here to validate SHA1 implementation. For this, I suggest using test vectors from NIST

There are test vectors for byte-oriented and bit-oriented implementations.

Example of a test vector from shabytetestvectors/SHA1ShortMsg.rsp file

Len = 152
Msg = 148de640f3c11591a6f8c5c48632c5fb79d3b7
MD = b47be2c64124fa9a124a887af9551a74354ca411

推荐阅读