I have viewed similar threads but unable to resolve this issue.
Here's my code:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <openssl/sha.h>
#define _CRT_SECURE_NO_WARNINGS
std::string sha256(const std::string str)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256); //HERE I AM RECEIVING ERROR
SHA256_Update(&sha256, str.c_str(), str.size());
SHA256_Final(hash, &sha256);
std::stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}
return ss.str();
}
void sha256_hash_string(unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65])
{
int i = 0;
for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
#pragma warning(suppress : 4996) sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
}
outputBuffer[64] = 0;
}
int main()
{
// hash a string
std::cout << "SHA-256 hash of \"Sample String\" text:" << std::endl;
std::cout << sha256("Sample String") << std::endl << std::endl;
// hash a file
return 0;
}
I have commented on the line where it is giving me the error:
Exception thrown at 0x00080000 in ConsoleApplication2.exe: 0xC0000005: Access violation executing location 0x00080000.
Linker configuration: linker>input
C/C++ Configuration: C/C++.general
Your build configuration shows that you're building in 32-bit mode i.e. Debug Win-32 but the library you're linking to is 64-bit. You need to set your build configuration as 64-bit, build the project again and run.
In addition, you need to copy the required DLLs in your build directory where your program binary resides if they're not in system path e.g. System32
. If you ship your application, take care of adding this step in your packaging system configuration.
User contributions licensed under CC BY-SA 3.0