#include "stdafx.h" #include "SiaCommon.h" #include #include NS_BEGIN(Sia) NS_BEGIN(Api) String AFX_EXT_API GenerateSha256(const String& str) { String ret; HCRYPTPROV hCryptProv = 0; HCRYPTHASH hHash = 0; BOOL ok = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, 0); ok = ok && CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash); ok = ok && CryptHashData(hHash, reinterpret_cast(&str[0]), str.length() * sizeof(TCHAR), 0); if (ok) { DWORD dwHashLen; DWORD dwCount = sizeof(DWORD); if (CryptGetHashParam(hHash, HP_HASHSIZE, reinterpret_cast(&dwHashLen), &dwCount, 0)) { std::vector hash(dwHashLen); if (CryptGetHashParam(hHash, HP_HASHVAL, reinterpret_cast(&hash[0]), &dwHashLen, 0)) { std::ostringstream ss; ss << std::hex << std::uppercase << std::setfill('0'); for (int c : hash) { ss << std::setw(2) << c; } std::string s = ss.str(); ret = CA2W(s.c_str()).m_psz; } } } if (hHash) CryptDestroyHash(hHash); if (hCryptProv) CryptReleaseContext(hCryptProv, 0); return ret; } NS_END(2)