How to hash strings in C using SHA256 algo

Need help with C, C++, perl, python, etc?

How to hash strings in C using SHA256 algo

Postby PsySc0rpi0n » 2019-11-10 21:31

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
User avatar
PsySc0rpi0n
 
Posts: 120
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to hash strings in C using SHA256 algo

Postby neuraleskimo » 2019-11-11 00:28

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
neuraleskimo
 
Posts: 115
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: How to hash strings in C using SHA256 algo

Postby PsySc0rpi0n » 2019-11-11 20:51

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: 120
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to hash strings in C using SHA256 algo

Postby PsySc0rpi0n » 2019-11-11 21:26

ok, I found the problem. I was using a required compilation flag mistyped. -lcrypt and it should be -lcrypto.
User avatar
PsySc0rpi0n
 
Posts: 120
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to hash strings in C using SHA256 algo

Postby reinob » 2019-11-12 10:48

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]);
reinob
 
Posts: 755
Joined: 2014-06-30 11:42

Re: How to hash strings in C using SHA256 algo

Postby PsySc0rpi0n » 2019-11-15 21:51

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: 120
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to hash strings in C using SHA256 algo

Postby PsySc0rpi0n » 2019-11-17 16:18

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?
User avatar
PsySc0rpi0n
 
Posts: 120
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to hash strings in C using SHA256 algo

Postby reinob » 2019-11-18 10:48

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.
reinob
 
Posts: 755
Joined: 2014-06-30 11:42

Re: How to hash strings in C using SHA256 algo

Postby neuraleskimo » 2019-11-18 13:39

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
neuraleskimo
 
Posts: 115
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: How to hash strings in C using SHA256 algo

Postby PsySc0rpi0n » 2019-11-25 21:25

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: 120
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to hash strings in C using SHA256 algo

Postby PsySc0rpi0n » 2019-11-25 21:26

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?
User avatar
PsySc0rpi0n
 
Posts: 120
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to hash strings in C using SHA256 algo

Postby neuraleskimo » 2019-11-26 00:48

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!
User avatar
neuraleskimo
 
Posts: 115
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

fashionable