Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8290527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:50:26+00:00 2026-06-08T12:50:26+00:00

Below is my code for creating a hash in openssl. I was wondering why

  • 0

Below is my code for creating a hash in openssl.
I was wondering why is my output always partially diffrent.

int main()
{
    char alg[] = "MD5";
    char buf[EVP_MAX_KEY_LENGTH] = "5";
    unsigned char *testKey;
    unsigned int olen;  

    testKey = simple_digest(alg, buf, EVP_MAX_KEY_LENGTH,&olen);

    std::cout << "Printing key : >>";
    print_hex(testKey,EVP_MAX_KEY_LENGTH);
    std::cout << "<<" << std::endl;
}

unsigned char *simple_digest(char *alg, char *buf, unsigned int len,unsigned int *olen)
{
    const EVP_MD *m;
    EVP_MD_CTX ctx;
    unsigned char *ret;
    OpenSSL_add_all_digests();

    if (!(m = EVP_get_digestbyname(alg)))
        return NULL;
    if (!(ret = (unsigned char *)malloc(EVP_MAX_MD_SIZE)))
        return NULL;


    EVP_DigestInit(&ctx, m);
    EVP_DigestUpdate(&ctx, buf, len);
    EVP_DigestFinal(&ctx, ret, olen);

//  std::cout << "computed key" << ret << std::endl;

    return ret;
} 

void print_hex(unsigned char *bs, unsigned int n)
{
    int i;
    for (i = 0; i < n; i++)
        printf("%02x", bs[i]);
    printf("\n");
}

Output :

3afb8ebc9c93bf6d40285736f210b7856af8bab4d040ca090043ca09f840ca09
3afb8ebc9c93bf6d40285736f210b7856af8bab490a5f909c0a7f909b8a5f909

As you can see only the back few characters differ.

Thanks in advance! 😀


Update (correct working version) :

int main()
{
    char alg[] = "MD5";
    char buf[EVP_MAX_KEY_LENGTH] = "5";
    unsigned char *testKey;
    unsigned int olen;  

    testKey = simple_digest(alg, buf,strlen(buf),&olen);

    std::cout << "Printing key : >>";
    print_hex(testKey,olen);
    std::cout << "<<" << std::endl;

}

unsigned char *simple_digest(char *alg, char *buf, unsigned int len,unsigned int *olen)
{
    const EVP_MD *m;
    EVP_MD_CTX ctx;
    unsigned char *ret;
    OpenSSL_add_all_digests();

    if (!(m = EVP_get_digestbyname(alg)))
        return NULL;
    if (!(ret = (unsigned char *)malloc(EVP_MAX_KEY_LENGTH)))
        return NULL;

    EVP_DigestInit(&ctx, m);
    EVP_DigestUpdate(&ctx, buf, len);
    EVP_DigestFinal(&ctx, ret, olen);

    return ret;
} 

void print_hex(unsigned char *bs, unsigned int n)
{
    int i;
    for (i = 0; i < n; i++)
        printf("%02x", bs[i]);
}

output :

Printing key : >>e4da3b7fbbce2345d7772b0674a318d5<<
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:50:28+00:00Added an answer on June 8, 2026 at 12:50 pm

    You are trying to hash a one-character string, “5”, but you are telling EVP_Digest_Update that your buffer length is EVP_MAX_KEY_LENGTH. You need to pass the actual length of your buffer, not the maximum length it could be.

    For completeness, here is what your main function should look like:

    int main()
    {
        char alg[] = "MD5";
        char buf[EVP_MAX_KEY_LENGTH] = "5";
        unsigned char *testKey;
        unsigned int olen;  
    
        # Pass the true length of but
        testKey = simple_digest(alg, buf, strlen(buf), &olen);
    
        std::cout << "Printing key : >>";
        # Pass the true length of testKey
        print_hex(testKey,olen);
        std::cout << "<<" << std::endl;
    }
    

    One other caveat: you probably don’t want to use EVP_MAX_KEY_LENGTH for your buffer, as that would limit the size of the message you could hash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below is creating a server to communicate with clients.This code works fine
I am creating excel file using below code public class ResultSetToExcel { private HSSFWorkbook
I am creating a chunk of HTML/JavaScript with the below code: $result = mysql_query(SELECT
I am creating a random ID using the below code: from random import *
In the code below is the initial first loop needed when creating a 2D
I am creating a dropdown contact form and you can view the code below:
I'm having some trouble with looping and creating MS Excel docs, code snippet below
I am creating a form in CakePHP.A snippet of the code is given below.
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
Below is my code for creating a symlink of a directory: sudo ln -s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.