Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want some help with the implementation of sha256 and ripemd160 hash in the c program. Does anyone know how can I perform sha256 on a char array? I wrote a code

<pre>#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
 
int main (void) {
	const char s[131] = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
	unsigned char *d = SHA256(s, strlen(s), 0);
 
	int i;
	for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
		printf("%02x", d[i]);
	putchar('\n');
 
	return 0;
}


the problem is this code gives a wrong output when the length of char array s increases.
If you replace the public key with any string which is small enough it will work correctly.
But I can't figure out how can I do it with big char arrays. Any suggestions or code would be helpful.
And also want help with how to do ripemd160 hash on the resulting outcome of sh256 in c language.
You can compile the above code with this command
gcc test.c -lssl -lcrypto


What I have tried:

<pre lang="text"><pre>include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
 
int main (void) {
	const char s[131] = "036EAF0968AA895ADDFEE599566F0B880242461D1377F4887C9B84631E13067B96";
	unsigned char *d = SHA256(s, strlen(s), 0);
 
	int i;
	for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
		printf("%02x", d[i]);
	putchar('\n');
 
	return 0;
}


the outcome of this cod is
d1b9207a2144271601f7fab515d60aeef167c919a1f78560314106576ada35ec

which is correct.
but dose not work with uncompressed form of public key of bitcoin.
Posted
Updated 19-Mar-20 5:07am

1 solution

You seem to be a little confused:
Quote:
If you replace the public key with any string which is small enough it will work correctly.

SHA is a hashing algorithm, not an encryption algorithm - it doesn't use a key, public or private.
Hashing algorithms always generate a value with the same size - that's the whole point - and they do that by "throwing away" information, and as such are not "reversible": you cannot guarantee to get the original input back from the output value. Encryption algorithms are different - they use a key to make the output data much, much harder to reverse back to get the original input.
If you code is any good for generating SHA values, then it should work for any size input. So at a guess, you are assuming something wrongly, or trying to do something that isn't actually possible.
 
Share this answer
 
Comments
Asish6542165 19-Mar-20 11:12am    
can you suggest me a code for performing sha256 on char array in c
OriginalGriff 19-Mar-20 11:25am    
You already have code for that:

SHA256(s, strlen(s), 0);

It's part of the OpenSSL library, and that is what it does!
https://www.openssl.org/docs/man1.0.2/man3/SHA256.html
Asish6542165 19-Mar-20 11:26am    
then why does it go wrong when I give it a char array of bigger size
OriginalGriff 19-Mar-20 11:30am    
How would I know? I can't see the code you use that uses a longer array!
And how do you know it goes wrong?
How are you testing it?
Asish6542165 19-Mar-20 11:31am    
yes i have tested it

it is the same code that you are looking for with the longer string 0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900