Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

How to hash strings in C using SHA256 algo

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

How to hash strings in C using SHA256 algo

#1 Post by PsySc0rpi0n »

Hello.

I'm using Debian Buster and I want trying to hash a string in a small C program.
What means can I use to accomplish this?

Thanks
Psy

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: How to hash strings in C using SHA256 algo

#2 Post by neuraleskimo »

Try libmhash, OpenSSL, or GNUTLS. Although you didn't ask for C++, I would steer a beginner to Qt's QCryptographicHash. Good luck!

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to hash strings in C using SHA256 algo

#3 Post by PsySc0rpi0n »

neuraleskimo wrote:Try libmhash, OpenSSL, or GNUTLS. Although you didn't ask for C++, I would steer a beginner to Qt's QCryptographicHash. Good luck!
I'm trying OpenSSL now but I'm getting errors in compiling the code.

I'm not a C++ guy at all. I just don't feel attracted by it's code at all.

Code: Select all

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

 #define BUFFER 256

 int main(void){
    const unsigned char* data_to_hash = NULL;
    char* hash = NULL;

    data_to_hash = calloc(SHA256_DIGEST_LENGTH, sizeof(const unsigned char));
    hash = calloc(SHA256_DIGEST_LENGTH, sizeof(char));

    printf("Enter string:\n");
    fgets((char*) data_to_hash, SHA256_DIGEST_LENGTH, stdin);
    size_t len = strlen((const char*) data_to_hash);
    SHA256(data_to_hash, len, (unsigned char*) hash);

    printf("Hash: %s\n", hash);
    return 0;
 }
I get the following error:
$ agcc -lcrypt -o test test.c
/usr/bin/ld: /tmp/ccjoZYK0.o: in function `main':
/home/narayan/Documents/BlockchainApps/test.c:18: undefined reference to `SHA256'
collect2: error: ld returned 1 exit status
It says undefined reference to SHA256 but it is defined in OpenSSL docs:
here

Code: Select all

#include <openssl/sha.h>

 int SHA1_Init(SHA_CTX *c);
 int SHA1_Update(SHA_CTX *c, const void *data, size_t len);
 int SHA1_Final(unsigned char *md, SHA_CTX *c);
 unsigned char *SHA1(const unsigned char *d, size_t n,
                     unsigned char *md);

 int SHA224_Init(SHA256_CTX *c);
 int SHA224_Update(SHA256_CTX *c, const void *data, size_t len);
 int SHA224_Final(unsigned char *md, SHA256_CTX *c);
 unsigned char *SHA224(const unsigned char *d, size_t n,
                       unsigned char *md);

 int SHA256_Init(SHA256_CTX *c);
 int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
 int SHA256_Final(unsigned char *md, SHA256_CTX *c);
 unsigned char *SHA256(const unsigned char *d, size_t n,
                       unsigned char *md);

 int SHA384_Init(SHA512_CTX *c);
 int SHA384_Update(SHA512_CTX *c, const void *data, size_t len);
 int SHA384_Final(unsigned char *md, SHA512_CTX *c);
 unsigned char *SHA384(const unsigned char *d, size_t n,
                       unsigned char *md);

 int SHA512_Init(SHA512_CTX *c);
 int SHA512_Update(SHA512_CTX *c, const void *data, size_t len);
 int SHA512_Final(unsigned char *md, SHA512_CTX *c);
 unsigned char *SHA512(const unsigned char *d, size_t n,
                       unsigned char *md);
I don't know what am I missing.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to hash strings in C using SHA256 algo

#4 Post by PsySc0rpi0n »

ok, I found the problem. I was using a required compilation flag mistyped. -lcrypt and it should be -lcrypto.

reinob
Posts: 1195
Joined: 2014-06-30 11:42
Has thanked: 99 times
Been thanked: 47 times

Re: How to hash strings in C using SHA256 algo

#5 Post by reinob »

Please note that you're defining "data_to_hash" as being able to hold SHA256_DIGEST_LENGTH -- that is 32 -- characters.

Given that you've defined BUFFER to be 256, I assume you want data_to_hash to be that size (you'd need to amend the fgets() call correspondingly).

Plus you surely don't want to just printf() the hash as a string, as the "characters" will range from 0 to 255, i.e. will generally not be printable.

You can do something like

Code: Select all

    for(unsigned int j = 0; j < SHA256_DIGEST_LENGTH; j ++)
	printf("%02hhX", hash[j]);

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to hash strings in C using SHA256 algo

#6 Post by PsySc0rpi0n »

Hi...

Sorry, I've been busy at work.

I see what you mean regarding the string size. I'll change it to SHA256_DIGEST_LENGTH.

I'll try the printf suggestion to see what comes out.

Thanks

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to hash strings in C using SHA256 algo

#7 Post by PsySc0rpi0n »

reinob wrote:Please note that you're defining "data_to_hash" as being able to hold SHA256_DIGEST_LENGTH -- that is 32 -- characters.

Given that you've defined BUFFER to be 256, I assume you want data_to_hash to be that size (you'd need to amend the fgets() call correspondingly).

Plus you surely don't want to just printf() the hash as a string, as the "characters" will range from 0 to 255, i.e. will generally not be printable.

You can do something like

Code: Select all

    for(unsigned int j = 0; j < SHA256_DIGEST_LENGTH; j ++)
	printf("%02hhX", hash[j]);
Ok, I tried that and it does what I was looking, but it handles chars individually. How can I pick up each char and save it into a string?

reinob
Posts: 1195
Joined: 2014-06-30 11:42
Has thanked: 99 times
Been thanked: 47 times

Re: How to hash strings in C using SHA256 algo

#8 Post by reinob »

PsySc0rpi0n wrote:
reinob wrote: You can do something like

Code: Select all

    for(unsigned int j = 0; j < SHA256_DIGEST_LENGTH; j ++)
	printf("%02hhX", hash[j]);
Ok, I tried that and it does what I was looking, but it handles chars individually. How can I pick up each char and save it into a string?
When you wrote "I'm not a C++ guy at all" I took it to mean that you're a "C guy", but apparently you still have a long way to learn :)

So we'll leave that as an exercise OK?
(do ask if you have a specific problem, after you have read the C book and maybe google'd a bit :)

As a mere hint, you could use sprintf to store instead of printing.
The code would be elegant, but arguably bloated. Another option is to calculate, for each byte of the hash, the corresponding two ASCII characters of the hexadecimal representation, like:

Code: Select all

for(pos = 0; pos < SHA256_DIGEST_LENGTH; pos ++) {
 string[2*pos] = ASCII code of higher 4-bits of hash[pos]
 string[2*pos+1] = ASCII code of lower 4-bits of hash[pos]
}
Hmm.. I think I wrote too much.
Good luck.

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: How to hash strings in C using SHA256 algo

#9 Post by neuraleskimo »

I am happy to give pointers too, but I just don't have the bandwidth to teach C one compiler/linker error at a time. (I don't mean that as harsh as it probably sounds.) As reinob said K&R is your best starting point. However, let's take a slightly different approach...

Why use C for your program? There are other languages that you could use. If you want to learn C, that is a great reason! However, hashing strings may not be the best starter program.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to hash strings in C using SHA256 algo

#10 Post by PsySc0rpi0n »

reinob wrote:
PsySc0rpi0n wrote:
reinob wrote: You can do something like

Code: Select all

    for(unsigned int j = 0; j < SHA256_DIGEST_LENGTH; j ++)
	printf("%02hhX", hash[j]);
Ok, I tried that and it does what I was looking, but it handles chars individually. How can I pick up each char and save it into a string?
When you wrote "I'm not a C++ guy at all" I took it to mean that you're a "C guy", but apparently you still have a long way to learn :)

So we'll leave that as an exercise OK?
(do ask if you have a specific problem, after you have read the C book and maybe google'd a bit :)

As a mere hint, you could use sprintf to store instead of printing.
The code would be elegant, but arguably bloated. Another option is to calculate, for each byte of the hash, the corresponding two ASCII characters of the hexadecimal representation, like:

Code: Select all

for(pos = 0; pos < SHA256_DIGEST_LENGTH; pos ++) {
 string[2*pos] = ASCII code of higher 4-bits of hash[pos]
 string[2*pos+1] = ASCII code of lower 4-bits of hash[pos]
}
Hmm.. I think I wrote too much.
Good luck.
Hi...

Sorry the late reply. I've been busy with something else.
Yes, I'm not a C++ guy. I'm way more a C guy but that doesn't mean I'm an expert or that I have much experience.
Of course I always want to learn more. I think I can admit I was a bit lazy about the question I made! To be honest I spent only a couple of minutes trying to figure out out to do what I wanted to do and then I just came here to ask how could I do it.
So I take the comment and thanks for it. I really thank it because I'm the first one agreeing with not giving straight answers to the problem. Rather teach the way to get there!

I'll dig into it and I'm sure I'll come up with a solution. I know it's quite easy but I was really lazy that day!
To be honest, my knowledge is more towards embedded programming (I mean that I like more and I read more about this type of programming, not that I am experienced in embedded programming). So, there are some concepts I tend to forget about this more common programming such as dealing with strings, etc.

Thanks
Psy

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to hash strings in C using SHA256 algo

#11 Post by PsySc0rpi0n »

neuraleskimo wrote:I am happy to give pointers too, but I just don't have the bandwidth to teach C one compiler/linker error at a time. (I don't mean that as harsh as it probably sounds.) As reinob said K&R is your best starting point. However, let's take a slightly different approach...

Why use C for your program? There are other languages that you could use. If you want to learn C, that is a great reason! However, hashing strings may not be the best starter program.
I didn't understand what you mean with "I am happy to give pointers too..." You mean C pointers or that you like to point me in the right direction for the solution I need?

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: How to hash strings in C using SHA256 algo

#12 Post by neuraleskimo »

PsySc0rpi0n wrote:I didn't understand what you mean with "I am happy to give pointers too..." You mean C pointers or that you like to point me in the right direction for the solution I need?
Sorry, I meant "point you in the right direction". Also, I want to try again on the other part... What I meant is I don't have much spare time right now for anything other than pointing to other people's information. I always enjoy helping others learn (repay the investment my teachers, mentors, family, and friends. made in me) by providing example code/explanations, help with debugging, etc. I was trying (poorly) to encourage you to keep asking questions, but if I disappear for a while, don't take it personally. I do care, but two family medical emergencies in the last six months have left me with little free time. Sorry to be an ass.

Of course, I and many others here can help with C pointers too!

Post Reply