首页 > 解决方案 > 从单词列表和哈希中读取的有效方法?

问题描述

我有两个功能,我该如何优化这段代码?在我使用 fscanf 从文件中读取的 wordlist 函数中,我使用一个临时数组来进行哈希处理,所以当我必须打印它时我可以保留读取的行,然后我将传递的摘要与计算的摘要进行比较(破解它) . 在 md5 函数中,我有带有 MD5 函数返回值的“结果”数组,然后我将数组复制到一个字符串中,因为我必须从 char 数组转换为十六进制字符串。有什么不对?这些功能有效,但我可以修复一些东西以更快或更正确地完成它吗?

void wordlistMD5(char digest[], char hashtype[], FILE *wordlist) {
    char line [512];
    while (fscanf(wordlist,"%s\n",line) != -1) {
        char hash[512] = {0};
        strncpy(hash,line,strlen(line));
        if (strcmp(md5(hash), digest) == 0) {
            printf("Found!\nhash %s %s -> %s\n", hashtype, digest, line);
            return;
        }
    }
    printf("Found nothing.\n");
    fclose(wordlist);
}

char *md5(char *string){
    unsigned char result[MD5_DIGEST_LENGTH];
    int i;
    MD5((const unsigned char *)string, strlen(string), result);
    for(i = 0; i < MD5_DIGEST_LENGTH; i++)
        sprintf(string + 2*i,"%02x",result[i]);
    return string;
}

标签: arrayscstringhashmd5

解决方案


推荐阅读