简体   繁体   English

C ++:如何将带有md5哈希的wstring转换为byte *数组?

[英]C++: How to convert wstring with md5 hash to byte* array?

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

//....

byte digest[16];

How can I get my md5 hash in digest? 如何在摘要中获取md5哈希? My answer is: 我的答案是:

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));
}

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: 您需要从hashStr读取两个字符,将其从十六进制转换为二进制值,然后将该值放入digest的下一个位置-顺序如下:

for (int i=0; i<16; i++) {
    std::wstring byte = hashStr.substr(i*2, 2);
    digest[i] = hextobin(byte);
}

C-way (I didn't test it, but it should work (though I could've screwed up somewhere) and you will get the method anyway). 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)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM