c++ - 带有 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 校验和的最佳长度是多少?
谢谢
解决方案
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
推荐阅读
- java - 最佳 OOP 方法和实践
- android - 键盘打开时列不滚动
- amazon-web-services - 使用 CDK 部署的 AppSync 失败,原因是:`命名的“AWSTimeStamp”标量类型没有标量实现。`
- c# - Docker-Compose 连接被 ASP-NET Core 5 拒绝
- postgresql - PostgreSQL:按引用字段值搜索
- python-3.x - 摆脱 pyside2 中的 QAction.trigger.connect
- php - 如何捕获 Doctrine\DBAL\Exception\ConnectionException?
- node.js - Mongoose Geolocation 设置默认坐标
- c# - 在与类型名称不匹配的文件名下划线
- ios - 无法找到为什么苹果拒绝我的构建的问题