1

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

linker > general

C/C++ Configuration: C/C++.general

  • 1
    Looks like the copied code from this [thread](https://stackoverflow.com/a/2458382/7670262). Are you sure you're using the correct versions of the crypto runtime library according to your VS version? – Azeem Mar 30 '20 at 05:57
  • It works fine in Cygwin. Prints out `87207aa0ffd69fc6203c68b98c09e552a4d7be46e1a4c9b723f06c3afc52810f` as the hash. – selbie Mar 30 '20 at 06:00
  • 1
    I think Azeem's suggestion might be viable. You obviously are linking with the openssl libraries, but when you run your compiled EXE it could be picking up another compiled DLL version elsewhere in your PATH. Make sure that you copy all the relevant OpenSSL dlls to the same directory as your EXE. Or compile OpenSSL as a static library and link to that. For openssl 1.0, this is libeay32.dll and ssleay32.dll. Might be different on OpenSSL 1.1 – selbie Mar 30 '20 at 06:02
  • Yes, I am following code from this website for installing and using opensssl: https://www.technical-recipes.com/2014/using-openssl-sha-256-in-visual-c/. I do not know know my crypto runtime library versions. – Ayesha Malik Mar 30 '20 at 06:06
  • Always build OpenSSL yourself. It's pretty easy for Windows. Don't rely on someone else's local build and package to do it correctly for you. – selbie Mar 30 '20 at 06:08
  • @AyeshaMalik: Can you add your build and link configuration in your question? – Azeem Mar 30 '20 at 06:10
  • Grabbing random DLLs off the Internet and shipping those to customers is FIPS compliant? There's no way that could be compliant with any security process. Openssl.org itself doesn't actually ship binaries. The tell everyone to build the package themselves. And who said anything about FIPS anyway? There's several sites that offer pre-compiled OpenSSL binaries for Windows developers. But it would be a good assumption to assume it's being provided by a rogue nation-state, the CIA, the NSA, or your competitor with a backdoor added. :) – selbie Mar 30 '20 at 06:14
  • @AyeshaMalik: Your build configuration shows Debug Win 32. Can you change it to Win 64, build it again and test? Plus, you need to copy those DLLs in your build directory where your program binary resides. – Azeem Mar 30 '20 at 06:29
  • Thank you so much, changing to Win64 works! I am new user and unable to vote or anything. – Ayesha Malik Mar 30 '20 at 06:45

1 Answers1

0

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.

Azeem
  • 11,148
  • 4
  • 27
  • 40