Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I wrote a c program to perform sha256 hash on a char array and then perform ripemd160 hash on the output of the sha256 hash function.

here is my code:
#include <stdio.h>
#include <string.h>
#include <openssl sha.h="">
#include <openssl ripemd.h="">

int main(){
int c;

const unsigned char* rawdata = "046EAF0968AA895ADDFEE599566F0B880242461D1377F4887C9B84631E13067B96DB18C41E0C208F8D12EBCC3F99F2522903AF6105833E4CBADE9D6A1D0F039187";
unsigned long n = strlen(rawdata);
unsigned char *d = SHA256(rawdata, strlen(rawdata), 0);

for (c = 0; c < 32; c++){
    printf("%02x", d[c]);
}
putchar('\n');

unsigned char md[32];
unsigned char* pmd = RIPEMD160(d, strlen(d), md);

int i;
for (i = 0; i < 20; i++)
    printf("%02x", pmd[i]);
putchar('\n');

return 0;
}

the problem is in this line :

unsigned char *d = SHA256(raw-data, strlen(raw-data), 0); when I pass the d pointer which is the output of the sha256 function to RIPEMD160 function the resulting hash output of RIPEMD160 function becomes wrong. Can anyone tell me why this is happening? And how can correct my code to print and store my ripems160 hash in a char array?

What I have tried:

Here is what I have tried:

the resulting hash output of the string "046EAF0968AA895ADDFEE599566F0B880242461D1377F4887C9B84631E13067B96DB18C41E0C208F8D12EBCC3F99F2522903AF6105833E4CBADE9D6A1D0F039187"

is : 37a0df85d5ccf7cb5f92b53aa3f223d76c115a844ed52d8978deecd2ecb3e406

which is correct.

But the ripemd160 hash of "37a0df85d5ccf7cb5f92b53aa3f223d76c115a844ed52d8978deecd2ecb3e406" should be

"4ecc9d3eea56b0af96b6db612b76911858dcb40d" but my is wrong.

here is the output of my code when compiled with this command

"gcc sha256.c -lssl -lcrypto -Wno-deprecated-declarations"

output:

37a0df85d5ccf7cb5f92b53aa3f223d76c115a844ed52d8978deecd2ecb3e406 63bb23be08e2c097008c4c272cc56c14e5656831

the second string "63bb23be08e2c097008c4c272cc56c14e5656831" is ripemd160 hash which is wrong
Posted
Updated 20-Mar-20 10:55am
v2
Comments
Richard MacCutchan 21-Mar-20 4:44am    
How do you know it is wrong?

1 solution

C
const unsigned char* rawdata = "046EAF0968AA895ADDFEE599566F0B880242461D1377F4887C9B84631E13067B96DB18C41E0C208F8D12EBCC3F99F2522903AF6105833E4CBADE9D6A1D0F039187";

You've already been told that, but I am going to tell you once more: the rawdata variable does not contain the byte values you think it does. Because you are confounding a byte array (which is a collection of byte values) and its string representation.
You may want to write instead:
C
const unsigned char* rawdata = { 0x04, 0x6E, 0xAF, 0x09, 0x68, 0xAA, 0x89, 0x5A, 0xDD, 0xFE, 0xE5, 0x99, 0x56, 0x6F, 0x0B, 0x88, 0x02, 0x42, 0x46, 0x1D, 0x13, 0x77, 0xF4, 0x88, 0x7C, 0x9B, 0x84, 0x63, 0x1E, 0x13, 0x06, 0x7B, 0x96, 0xDB, 0x18, 0xC4, 0x1E, 0x0C, 0x20, 0x8F, 0x8D, 0x12, 0xEB, 0xCC, 0x3F, 0x99, 0xF2, 0x52, 0x29, 0x03, 0xAF, 0x61, 0x05, 0x83, 0x3E, 0x4C, 0xBA, 0xDE, 0x9D, 0x6A, 0x1D, 0x0F, 0x03, 0x91, 0x87 };

In other words: The string representation of a byte array is stored internally with a collection of bytes which are not the same as the bytes in the byte array itself.

Short example:
Take the byte array { 0x00, 0x42 }. It has 2 bytes.
It's string representation is "0042", which has 4 characters, and is stored in memory with 4 bytes ({ 0x30, 0x30, 0x34, 0x32 }, which are the ASCII codes of characters under hexadecimal representation), i.e. twice as the value it is representing.
 
Share this answer
 
v2
Comments
OriginalGriff 20-Mar-20 17:13pm    
I get the feeling this one doesn't want to learn anything - just have solutions handed to him. :sigh:
phil.o 20-Mar-20 17:18pm    
You may be right, but who knows, a spark might fly.
Asish6542165 22-Mar-20 8:23am    
but when I do it while compiling it gives me a segmentation fault
phil.o 22-Mar-20 8:29am    
A segmentation fault happens when trying to access a memory address which you don't own. Which line is causing this to happen?
Try to use an array declaration instead of a pointer:
const unsigned char rawdata[] = { 0x04, 0x6E, ... };

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