Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <Windows.h>
- #include <wincrypt.h>
- using namespace std;
- // вывод ошибок
- void ErrorHandling(string message)
- {
- DWORD dw = GetLastError();
- cout << "Error with: " << message << "\n";
- cout << "Error code: " << dw << "\n";
- }
- int main()
- {
- HCRYPTPROV hCryptProv;
- HCRYPTHASH hHash;
- HCRYPTHASH hDuplicatedHash;
- if (CryptAcquireContext(
- &hCryptProv,
- nullptr,
- nullptr,
- PROV_RSA_FULL,
- 0))
- {
- cout << "CryptAcquireContext complete. \n";
- }
- else
- {
- ErrorHandling("Acquisition of context failed.");
- }
- // получить дескриптор хэш объекта
- if (CryptCreateHash(
- hCryptProv,
- CALG_MD5,
- 0,
- 0,
- &hHash))
- {
- cout << "An empty hash object has been created. \n";
- }
- else
- {
- ErrorHandling("CryptBeginHash");
- }
- string input = "qwerty";
- if (CryptHashData(
- hHash, reinterpret_cast<const BYTE*>(input.c_str()), input.size(),0
- ))
- {
- cout << "CryptHashData executed successfully\n";
- }
- else
- {
- ErrorHandling("CryptHashData");
- }
- if (CryptDuplicateHash(
- hHash,
- nullptr,
- 0,
- &hDuplicatedHash
- ))
- {
- cout << "The hash has been duplicated. \n";
- }
- else
- {
- ErrorHandling("CryptDuplicateHash");
- }
- BYTE* pbHash;
- DWORD dwHashLen;
- DWORD dwHashLenSize = sizeof(DWORD);
- if (CryptGetHashParam(
- hDuplicatedHash,
- HP_HASHSIZE,
- reinterpret_cast<BYTE*>(&dwHashLen),
- &dwHashLenSize,
- 0
- ))
- {
- cout << "CryptGetHashParam HP_HASHSIZE executed successfully\n";
- }
- else
- {
- ErrorHandling("CryptGetHashParam failed to get size.");
- }
- if ((pbHash = static_cast<BYTE*>(malloc(dwHashLen))))
- cout << "Allocation pbHash success\n";
- else
- cout << "Allocation failed\n";
- string res;
- if (CryptGetHashParam(
- hHash,
- HP_HASHVAL,
- pbHash,
- &dwHashLen,
- 0))
- {
- if (pbHash == nullptr)
- {
- cout << "system error\n";
- exit(1);
- }
- // первый вариант вывода
- cout << "1 hash is: ";
- for (size_t i = 0; i!= 16; ++i)
- {
- res += "0123456789ABCDEF"[pbHash[i] / 16];
- res += "0123456789ABCDEF"[pbHash[i] % 16];
- }
- cout << res << "\n";
- // второй вариант вывода
- cout << "2 hash is: ";
- for(int i = 0 ; i < dwHashLen ; i++)
- {
- printf("%02x",pbHash[i]);
- }
- cout << "\n";
- }
- else
- {
- ErrorHandling("CryptGetHashParam\n");
- }
- if (hHash)
- CryptDestroyHash(hHash);
- if (hDuplicatedHash)
- CryptDestroyHash(hDuplicatedHash);
- if (hCryptProv)
- CryptReleaseContext(hCryptProv, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement