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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:55:36+00:00 2026-05-22T20:55:36+00:00

This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>

  • 0

This is the code

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <byteswap.h>
#include "/usr/include/openssl/sha.h"
#include <evhttp.h>

bool hex2bin(unsigned char *p, const char *hexstr, size_t len);

bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
{
while (*hexstr && len) {
    char hex_byte[3];
    unsigned int v;

    if (!hexstr[1]) {
        //applog(LOG_ERR, "hex2bin str truncated");
        return false;
    }

    hex_byte[0] = hexstr[0];
    hex_byte[1] = hexstr[1];
    hex_byte[2] = 0;

    if (sscanf(hex_byte, "%x", &v) != 1) {
        //applog(LOG_ERR, "hex2bin sscanf '%s' failed",
            //hex_byte);
        return false;
    }

    *p = (unsigned char) v;

    p++;
    hexstr += 2;
    len--;
}

return (len == 0 && *hexstr == 0) ? true : false;
}

int main(int argc, char **argv)
{
unsigned char hash[SHA256_DIGEST_LENGTH], hash1[SHA256_DIGEST_LENGTH];
uint32_t *hash32 = (uint32_t *) hash;
unsigned char data[128];
uint32_t *data32 = (uint32_t *) data;
int i;

   hex2bin(data, argv[1], sizeof(data));

for (i = 0; i < 128/4; i++)
    data32[i] = bswap_32(data32[i]);

SHA256(data, 80, hash1);
SHA256(hash1, SHA256_DIGEST_LENGTH, hash);

printf("%s\n\n",hash1);

return 0;   
}

Sorry for the long code. Also, don’t mind the unnecessary includes, i just wasn’t sure which were needed so i compiled regardless.

This is written in C(obviously) running under Linux, Ubuntu 11.04.

This program takes as an argument some string and must compute it’s sha256 hash(twice, while doing various stuff before actually hashing it).
Although the program compiles,runs and takes the string, it spews some weird characters instead of a hash.
Why so?

P.S
This code is not mine, i just made it into a separate program for myself, but like i said, printing the hash fails with some weird characters!

P.S2
SHA256 is defined in the LibSSL’s libcrypto library. The program is compiled with the -lcrypto argument using gcc.

  • 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-05-22T20:55:37+00:00Added an answer on May 22, 2026 at 8:55 pm

    The output of a hash is almost always opaque binary data – i.e. any bytes.

    You’re trying to print those as if they were text. That means it’ll be applying some encoding to the binary data, trying to interpret it as text.

    Basically you should use the opposite of hex2bin in order to convert the arbitrary binary data into hex.

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

Sidebar

Related Questions

Please have a look on this code: #include <unistd.h> #include <stdlib.h> #include <stdio.h> int
Just to test, I ran this code #include<unistd.h> #include<stdio.h> #include<stdlib.h> int main() { int
This code #include <stdio.h> #include <stdlib.h> #include <time.h> int main () { printf (First
I wrote this code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <sys/shm.h>
I am using this code: #include <stdlib.h> #include <stdio.h> #include <math.h> #include <cv.h> #include
I have this code: #include <stdio.h> #include <stdlib.h> #include <signal.h> // Define the function
have a look at this code: #include<stdio.h> #include <unistd.h> int main() { int pipefd[2],n;
I was under the impression that this code #include <windows.h> #include <stdio.h> int WINAPI
If I have a global static variable x like in this code #include <stdio.h>
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include

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.