簡體   English   中英

C ++:如何將帶有md5哈希的wstring轉換為byte *數組?

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

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

您需要從hashStr讀取兩個字符,將其從十六進制轉換為二進制值,然后將該值放入digest的下一個位置-順序如下:

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

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