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 9113727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:01:51+00:00 2026-06-17T04:01:51+00:00

I have been working on getting the sha1() function working from openssl/sha.h however I

  • 0

I have been working on getting the sha1() function working from openssl/sha.h however I am getting random output and some warning. I have read quite a bit and tried some of the example codes but I get warning on all of it and it doesn’t display correctly.

Here is code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <openssl/sha.h>

int main() 
{ 
const unsigned char data[] = "Hello, World";
unsigned long length = sizeof(data);
unsigned char  hash[SHA_DIGEST_LENGTH];
SHA1(data, length, hash);
printf("%02x \n", hash);
return 0;

}

Below is the warning I am getting:

sha.c: In function ‘main’:
sha.c:12: warning: ‘SHA1’ is deprecated (declared at /usr/include/openssl/sha.h:124)
sha.c:13: warning: format ‘%02x’ expects type ‘unsigned int’, but argument 2 has type   ‘unsigned char *’
sha.c:13: warning: format ‘%02x’ expects type ‘unsigned int’, but argument 2 has type ‘unsigned char *’

When I run it and I get the output:
62652b34

Any help would be great!

  • 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-17T04:01:52+00:00Added an answer on June 17, 2026 at 4:01 am

    It also took me a while before I figured it all out. The best way is to use EVP, it provides generic functions for almost everything.

    #include <openssl/evp.h>
    

    You need to call this im main before calling you hash function. To initialize your hashes. Otherwise openssl will complain that the algorithm is not available.

    OpenSSL_add_all_algorithms();
    

    mode must be “SHA256”, “SHA512”, “SHA1” as string.
    dataToHash is the input, dataSize is the size of the input,
    outHashed should already be allocated, the hash will be written there

    unsigned int hash(const char *mode, const char* dataToHash, size_t dataSize, unsigned char* outHashed) {
        unsigned int md_len = -1;
        const EVP_MD *md = EVP_get_digestbyname(mode);
        if(NULL != md) {
            EVP_MD_CTX mdctx;
            EVP_MD_CTX_init(&mdctx);
            EVP_DigestInit_ex(&mdctx, md, NULL);
            EVP_DigestUpdate(&mdctx, dataToHash, dataSize);
            EVP_DigestFinal_ex(&mdctx, outHashed, &md_len);
            EVP_MD_CTX_cleanup(&mdctx);
        }
        return md_len;
    }
    

    A use example (this is not tested, I use the above code in a c++ wrapper)

    const char *inData = "test data2";
    unsigned char outHash[20]; // output is already allocated
    hash("SHA1", inData, 10, outHash);
    

    You shouldn’t use the SHA1 method directly it is deprecated (your code could blow up with the next version).
    If you want to use your version you need to print each char as hex:

    int i;
    for(i=0; i<SHA_DIGEST_LENGTH; i++) {
        printf("%02x", hash[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on getting a mail app to work from a tutorial
I have been working on a project in scala, but I am getting some
Following on from my previous question I have been working on getting my object
I have been working on getting this seat mapping chart for a while and
I have been working on getting my OSGi application to run outside eclipse. It
I have been working on getting a basic ATL project to compile in Visual
Alright so i have been working on this Dynamic load of a spinner from
I have been working on getting log4net working in a demo program for a
So I have been working on getting this Twitter Bootstrap typeahead to work and
I have been working on an ASP.NET/C# web app for some time and its

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.