Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
just follow this link.

SHA256 and RIPEMD160HASH in C program[^]

when I replace

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


with

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 };


why does it give me a segmentation fault

What I have tried:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Posted
Updated 22-Mar-20 5:52am
v3

when you are using an initialization list (your 2nd case) you need to change your declaration to:

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 };
 
Share this answer
 
v2
looking back at your original question, you had
C++
const unsigned char* rawdata = "046EAF0968AA895ADDFEE599566F0B880242461D1377F4887C9B84631E13067B96DB18C41E0C208F8D12EBCC3F99F2522903AF6105833E4CBADE9D6A1D0F039187";
unsigned long n = strlen(rawdata);
unsigned char *d = SHA256(rawdata, strlen(rawdata), 0);

If all you've done is changed the initialization of rawdata to an array of hex constants, then strlen() is going to fail. strlen counts the number of bytes starting at the given pointer and continues until it finds a nul byte ('\0'). You could add a 0x00 to the end of the byte array, and then strlen will work, but be aware that if you have a situation where there is a zero byte in rawdata then you will get a short count for strlen.
If you use const unsigned char[], then the compiler knows the size of the array, even if you don't:
C++
const unsigned char rawdata[] = { 0x04, 0x6E, 0xAF, 0x09, 0x68, 0xAA, 0x89, 0x5A, 0xDD, /* etc */ };
const size_t n = sizeof rawdata; // value of n is number of bytes in rawdata

Since you need to know the number of bytes to pass SHA256, we can just use sizeof. If you need to know the number of elements in an array, you can use the following:
C++
long arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const size_t bytes_in_arr = sizeof arr;  // compile time calculation of bytes in arr (80 for x86_64)
const size_t elts_in_arr = sizeof arr/sizeof arr[0]; // compile time calculation of number of elements in arr (10 in this case)

Note that this will only work if you've declared your array with the [] notation. If you use the * notation (e.g. const char *data = "Hello World"), then sizeof data equals sizeof(char *), not the number of bytes in the initializing string.
 
Share this answer
 

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