Click here to Skip to main content
15,881,810 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I wrote a c program to calculate ripemd160 hash on a 64-byte char array.But it give me the wrong output. This is my code:

<pre>#include <stdio.h>
#include <string.h>
 #include <openssl/ripemd.h>
int main (void) {
	 unsigned char s[7] = "37a0df85d5ccf7cb5f92b53aa3f223d76c115a844ed52d8978deecd2ecb3e406";
	unsigned long n=20;
	unsigned char *d;


	 unsigned char *RIPEMD160(const unsigned char *s, unsigned long n,
                  unsigned char *d);
 
	int i;
	for (i = 0; i < 20; i++)
		printf("%02x", d[i]);
	putchar('\n');
 
	return 0;
}



can anyone tell me where am I am going wrong?

output for this code is:
31ed4989d15e4889e24883e4f050544c8d059a01


but the correct output should be:
4ecc9d3eea56b0af96b6db612b76911858dcb40d


What I have tried:

#include <stdio.h>
#include <string.h>
 #include <openssl/ripemd.h>
int main (void) {
	 unsigned char s[7] = "37a0df85d5ccf7cb5f92b53aa3f223d76c115a844ed52d8978deecd2ecb3e406";
	unsigned long n=20;
	unsigned char *d;


	 unsigned char *RIPEMD160(const unsigned char *s, unsigned long n,
                  unsigned char *d);
 
	int i;
	for (i = 0; i < 20; i++)
		printf("%02x", d[i]);
	putchar('\n');
 
	return 0;
}
Posted
Updated 20-Mar-20 5:33am
Comments
Richard MacCutchan 20-Mar-20 9:50am    
You are declaring the RIPEMD160 function but you never call it to do the conversion.
Asish6542165 20-Mar-20 10:15am    
how can I call it? It should be declared in OpenSSL right? Please tell me how can i do it.
Richard MacCutchan 20-Mar-20 10:41am    
The same way you call any function. Look at the documentation, set the correct parameters, and capture any response data. You have also declared the array s as only 7 characters, so that code should not even compile. I cannot understand where you are getting result values from.
Asish6542165 20-Mar-20 10:47am    
can you give me some c code to perform ripemd160 hash on a char array in c language? It would be really helpful. please
Richard MacCutchan 20-Mar-20 11:12am    

Quote:
31ed4989d15e4889e24883e4f050544c8d059a01
If that is the hexadecimal string representation of an array of bytes then you should do something like:
C
unsigned char s[] = { 0x31, 0xed, 0x49, 0x89, ..., 0x01};
unsigned char h[20];
RIPEMD160(s, sizeof(s), h);

(you should replace the ... with the actual numbers, of course).
 
Share this answer
 
Comments
Asish6542165 20-Mar-20 9:21am    
this gives an error.

test.c: In function ‘main’:
test.c:9:1: warning: ‘RIPEMD160’ is deprecated [-Wdeprecated-declarations]
9 | RIPEMD160(s, sizeof(s), h);
| ^~~~~~~~~
In file included from /usr/local/include/openssl/opensslv.h:109,
from /usr/local/include/openssl/macros.h:11,
from /usr/local/include/openssl/opensslconf.h:14,
from /usr/local/include/openssl/macros.h:10,
from /usr/local/include/openssl/ripemd.h:14,
from test.c:3:
/usr/local/include/openssl/ripemd.h:49:1: note: declared here
49 | DEPRECATEDIN_3_0(unsigned char *RIPEMD160(const unsigned char *d, size_t n,
| ^~~~~~~~~~~~~~~~

and the string "31ed4989d15e4889e24883e4f050544c8d059a01" is actually a sha256 of another input. I am trying to write a c code to generate bitcoin addresses. I cannot pass it in this unsigned char s[] = { 0x31, 0xed, 0x49, 0x89, ..., 0x01}; way.this string is actually stored in a pointer variable.
Asish6542165 20-Mar-20 11:12am    
can you give me some c code to perform ripemd160 hash on a char array in c language? It would be really helpful. please
C++
const unsigned char* rawdata = "<add your string data here>";
unsigned long n = strlen(rawdata);
unsigned char md[32];
unsigned char* pmd = RIPEMD160(rawdata, n, md);
 
Share this answer
 
Comments
Asish6542165 20-Mar-20 12:24pm    
I have tried the same thing but I don't know why it keeps on giving me this error:

test.c: In function ‘main’:
test.c:10:1: warning: ‘RIPEMD160’ is deprecated [-Wdeprecated-declarations]
10 | unsigned char* pmd = RIPEMD160(rawdata, n, md);
| ^~~~~~~~
In file included from /usr/local/include/openssl/opensslv.h:109,
from /usr/local/include/openssl/macros.h:11,
from /usr/local/include/openssl/opensslconf.h:14,
from /usr/local/include/openssl/macros.h:10,
from /usr/local/include/openssl/ripemd.h:14,
from test.c:3:
/usr/local/include/openssl/ripemd.h:49:1: note: declared here
49 | DEPRECATEDIN_3_0(unsigned char *RIPEMD160(const unsigned char *d, size_t n,
Richard MacCutchan 20-Mar-20 12:36pm    
The compiler is telling you that this function is out of date and should not be used. Use a newer hash, such as SHA256, which is fully supported.
Asish6542165 20-Mar-20 12:39pm    
is there any way to use ripemd160 hash in the c program.
Richard MacCutchan 20-Mar-20 12:55pm    
That is not an error, it is a warning, the program should still compile. However, you can suppress it by adding the option -Wno-deprecated-declarations to your compiler options as shown in the message.
Asish6542165 20-Mar-20 13:43pm    
thanks a lot for you help

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