0

I am trying to create a program that prints the given hash as input.

Example: myprogram.exe
04033ca1921e1d4ab6c4036401786c18d97dca5ea4f886b95ae92d4e3423fffe0045071b97cdd00df725611d757d1ca756ab325230a7faeead09385663a29ae2

Output: Hash algorithm: SHA512

myprogram.exe
86896e2eb7ea8ce15c3037d4d59e2f5f505d28588b413b48499d54f6d83b877fc01512aac7c98f28bb2ef3f0b7ddd5e740ae3146d13385efe1bc67f00721c951

Output: Hash algorithm: SHA3-512

myprogram.exe
c872e20ea321f223c531c03a37b58c51

Output: Hash algorithm: MD5

I have this code:

#include <iostream>
#include <string>
#include <openssl/evp.h>

std::string determineHashAlgorithm(const std::string& hash) {
    const EVP_MD* md = EVP_get_digestbyname("SHA256");
    if (md == nullptr) {
        return "Unknown";
    }

    EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
    EVP_DigestInit_ex(mdctx, md, nullptr);
    unsigned char md_value[EVP_MAX_MD_SIZE];
    unsigned int md_len;
    EVP_DigestUpdate(mdctx, hash.c_str(), hash.length());
    EVP_DigestFinal_ex(mdctx, md_value, &md_len);
    EVP_MD_CTX_free(mdctx);

    if (md_len == hash.length() && memcmp(md_value, hash.c_str(), md_len) == 0) {
        return EVP_MD_name(md);
    }

    return "Unknown";
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cout << "Usage: " << argv[0] << " <hash>\n";
        return 1;
    }

    std::string hash = argv[1];
    std::string algorithm = determineHashAlgorithm(hash);

    std::cout << "Hash algorithm: " << algorithm << std::endl;

    return 0;
}

And it says: Hash algorithm: unknown every time, no mater the hash you give as input.

Thank you in advance!

10
  • 5
    This is not what these EVP functions do. They calculate a specific hash that you specify. That is, you specify whether you want to calculate an MD5 hash, a SHA1, hash, or a SHA256 hash, of something. Then you specify the something. And then these functions calculate the hash. There is no OpenSSL function that attempts to figure out what random hash was used to compute a hash of some unknown piece of data. In fact, this may not be always mathematically possible to do. Commented Jul 11, 2023 at 18:35
  • 3
    All that you can tell in general about a string of hex characters is the bit length, You can guess it is MD5 or SHA-1/2 by the length and them being the most common hash methods but it is still just guessing. It might not even be a hash, just binary data. Also, hashes are one-way and lossy so you can't then "decrypt" the hash. Commented Jul 11, 2023 at 18:38
  • 3
    If the hash algorithms are any good, you can't know. You would need to know the cleartext input, hash it with all the possible algorithms and see if you get the same hash value with any of them. Commented Jul 11, 2023 at 18:56
  • 1
    Who gave you this Hard task™ ? Why do you need to be able to do this? Commented Jul 11, 2023 at 18:58
  • 3
    "To help me with this assignment, please also provide me with a quantum computer" Commented Jul 11, 2023 at 19:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.