1
std::wstring hashStr(L"4727b105cf792b2d8ad20424ed83658c");

//....

byte digest[16];

如何在摘要中获取我的 md5 哈希?我的回答是:

wchar_t * EndPtr;

for (int i = 0; i < 16; i++) {
std::wstring bt = hashStr.substr(i*2, 2);
digest[i] = static_cast<BYTE>(wcstoul(bt.c_str(), &EndPtr, 16));
}
4

2 回答 2

1

You need to read two characters from hashStr, convert them from hex to a binary value, and put that value into the next spot in digest -- something on this order:

for (int i=0; i<16; i++) {
    std::wstring byte = hashStr.substr(i*2, 2);
    digest[i] = hextobin(byte);
}
于 2012-02-20T17:51:32.650 回答
0

C-way(我没有测试它,但它应该可以工作(尽管我可能在某个地方搞砸了),无论如何你都会得到这个方法)。

memset(digest, 0, sizeof(digest));

for (int i = 0; i < 32; i++)
{
    wchar_t numwc = hashStr[i];
    BYTE    numbt;

    if (numwc >= L'0' && numwc <= L'9')             //I assume that the string is right (i.e.: no SGJSGH chars and stuff) and is in uppercase (you can change that though)
    {
        numbt = (BYTE)(numwc - L'0');
    }
    else
    {
        numbt = 0xA + (BYTE)(numwc - L'A');
    }

    digest[i/2] += numbt*(2<<(4*((i+1)%2)));
}
于 2012-02-20T18:01:54.220 回答