【发布时间】:2016-04-02 01:57:26
【问题描述】:
我正在尝试构建一个 C 程序来查找与给定 SHA256 和等效的未知字符串。最终程序应该生成格式为“###ENCRYPTION”的字符串,然后找到它的 SHA 和并与已知值进行比较。但是,我在简单地生成 SHA 总和时遇到了问题。出于某种原因,我打印总和的方法丢失了 1 个字符:
000ENCRYPTION=f6bc212596f37e1855fb2bbfaf49b514c5ea79e332c57bb6**0**c493bafd38baff5
但我的代码生成:
000ENCRYPTION=f6bc212596f37e1855fb2bbfaf49b514c5ea79e332c57bb6c493bafd38baff5
This link 似乎描述了一个类似的问题,但它不太适合我的问题。
我已经用 Python 构建了一个程序来做同样的事情,但现在我想用 C 构建一个程序来比较运行时间。 (我也使用了 OS X 的 CommonCrypto 库,但 OpenSSL 也可以。)
任何帮助将不胜感激!
#include <stdio.h>
#include <CommonCrypto/CommonDigest.h>
#include <string.h>
int main() {
int c=0;
char *input_sum="9dcaea0ae17e31d47640cb7c390976d8962823a55ea03fe43b0d061f5624069f"; // currently unused
char *end="ENCRYPTION";
char temp[100]; // need to learn more about allocating memory
unsigned char output[32]; // initializes output variable
printf("Sum=f6bc212596f37e1855fb2bbfaf49b514c5ea79e332c57bb60c493bafd38baff5\n"); // Sum of 000ENCRYPTION from command line
while( c < 5 ) { // limited to 5 right now for testing
sprintf(temp, "%03d", c); // creates leading zeros
sprintf(temp + 3, "%s", end); // appends string "end"; should ideally be moved outside loop
c++;
puts(temp); // visual check of input into cc_sha256
CC_SHA256(temp, (CC_LONG)strlen(temp), output); // function does sha256sum
int i; // for loop prints resulting byte array from cc_sha256 to hex; I believe the problem is here
for (i = 0; i < 32; i++) {
printf("%x", output[i]);
}
printf("\n");
}
return 0;
}
【问题讨论】:
-
无论哪种方式都可以编译。我把两者都放在试图提高知名度
-
您确实意识到找到一个散列到特定 SHA256 散列的给定字符串可能需要平均 2^256 次试验,我猜?找到两个散列到相同值的不同字符串会稍微容易一些,但平均仍需要 2^255 次试验……即使你可以每秒尝试 1,000,000,000 次,也需要大约 1.8*10^60年完成...
-
在这种情况下,哈希字符串的格式是已知的(###ENCRYPTION),因此最多需要1000次尝试。