逐行散列文件时错误的md5哈希值

时间:2015-01-15 11:22:29

标签: c++ hash io md5 ifstream

我的程序尝试逐行散列文件 Passwords.txt 。但是,每次执行此操作时,所有哈希值都不正确。

void Rainbow::generateTable(string filename)
{
    //noOfLines=0;
    //bitsToRemove=0;

    ifstream myfile("Passwords.txt");

    if (myfile)
    {

        string line;

        while (getline(myfile, line))
        {
            noOfLines++;                         
        }
     }


    cout << "File is " << noOfLines << " lines long!" << endl;


    toRoundUp = log2(noOfLines);

    cout << "toRoundUp is " << toRoundUp << endl;
    bitsToRemove = ceil(toRoundUp);

    cout << "bitsToRemove is " << bitsToRemove << endl;

    //myfile.close();

    ifstream file("Passwords.txt");
    string myline;

    if (file.is_open())
    {



        while (getline(file,myline))
        {

            cout << md5(myline) << endl;


        }

    }




    displayMenu();


}

但是,当我尝试这样的事情时:哈希值是正确的

cout << md5("one") << endl;

Passwords.txt的内容如下:

one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty

这些是我的程序输出的哈希值:

945da772b285bb656550d0c2b94ef80c
9d6ec883255ab3527cd340baad797de5
e119d725f6a7b5413a61da21e7232cd5
31897146a8acc1eede32b7cbf4280f86
d97ee990314c796d0cfbedf39dc9d716
b27f9179c0570da135ec7c7f71c06692
b5e416cbaa6b63b46bf6975b2f365bfe
684349fb14f1cd243d91699a03c00b00
67ed2e62351f8e26e2ba17c30075f8be
41be321de3beb1474b2281bbbe4c8115
59bb31725256bd2af6d0b45d6372ff7a
ac573bd3385c4c0a19d5998bd16b3c0a
aae690fe1db712f89101e6665eccdb4a
93cfff3ce14123aa984447d33cd7fbe4
f35aa54743fa6bb0b8a319e55f8830ae
f516ed17f1fefe016cb3a3a398745682
f81da1a9d89b53a89fc71113ff9da7dd
aac5308f9cd346f9a11b9ff510ebd355
5178aa7f04124a2b930aa02abd9dc7f1
067636f2e9b0852519f85c2a33782b00

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

正如@Biffen所提到的,使用 nano 创建一个文件并通过Ubuntu GUI这样做会导致EOL的差异。因此,我需要做的就是丢弃现有文件,并通过Ubuntu GUI创建一个“相同”文件。