Hi,
I'm coding a hash algorithm (SHA-1) in C. The algorithm works, everything compiles and I seem to be getting the correct results when I simulate it. I obviously want to automate the checking proceedure so have obtained the necessary results from "hashcalc" and included them as several 20 by 8bit arrays within my code. The hash calc algorithm returns a 5 by 32bit array to an 8bit pointer, I'm then storing the data to a 20 by 8bit array using a do-while loop before passing pointers to this array along with the array containing the known results to another function which compairs the 2 arrays as can be seen in the below code: -
void main (void)
-
{
-
uint8_T sha1_check = 0;
-
uint8_T *sha1_op_ptr = 0;
-
uint8_T expected_sha1_result_1[20] = {0x3e,0x51,0x75,0x38,0x6b,0xe6,0x83,0xd2,0xf2,0x3,
-
0xf3,0xfa,0x3e,0xab,0x89,0x2a,0x79,0x90,0x82,0xf7}; /*data obtained from hash checker*/
-
-
-
sha1_op_ptr = (uint8_T *)calculateSHA1();
-
-
do{
-
res[loop] = *sha1_op_ptr++;
-
}while(loop++ < 20);
-
-
sha1_check = checkOutputSHA1((uint8_T *)&res[0],(uint8_T *)&expected_sha1_result[0]);
-
}
-
-
uint32_T calculateSHA1(void)
-
{
-
uint32_T hash_arr[5] = {0};
-
-
/* Perform Hash Algorithm */
-
-
return hash_arr;
-
}
-
-
void checkOutputSHA1(uint8_T *hash_res_ptr,uint8_T *result_from_hash_checker_ptr);
-
{
-
uint8_T matched_byte = 0;
-
uint8_T check_sha1_res_loop = 0;
-
uint8_T res[20] = 0;
-
-
-
do{
-
res[check_sha1_res_loop]= *hash_res_ptr;
-
if(*hash_res_ptr++ == *result_from_hash_checker_ptr++)
-
{
-
matched_byte++;
-
}
-
}while(check_sha1_res_loop++ < 20);
-
-
return matched_byte;
-
}
-
Now this works as matched_byte returns 20. However this approach is requiring me to carry out 2 do-while loops. 1 in main and 1 in checkOutputSHA1() which seems a bit pointless when I believe I could get away with just performing a do-while in checkOutputSHA1() if I passed sha1_op_ptr to checkOutputSHA1() instead of res[] as such.. -
void main (void)
-
{
-
uint8_T sha1_check = 0;
-
-
uint8_T *sha1_op_ptr = 0;
-
-
/*data obtained from hash checker*/
-
uint8_T expected_sha1_result_1[20] = {0x3e,0x51,0x75,0x38,0x6b,0xe6,0x83,0xd2,0xf2,0x31,
-
0xf3,0xfa,0x3e,0xab,0x89,0x2a,0x79,0x90,0x82,0xf7};
-
-
-
sha1_op_ptr = (uint8_T *)calculateSHA1();
-
-
do{
-
res[loop] = *sha1_op_ptr++;
-
}while(loop++ < 20);
-
-
sha1_check = checkOutputSHA1(sha1_op_ptr,(uint8_T *)&expected_sha1_result[0]);
-
}
-
Unfortunately this doesn't seem to work correctly as matched_byte = 0. Any suggestions as to what I'm doing wrong would be greatly appreciated as it's driving me nuts!!! Does the function prototype for checkOutputSHA() need modifying as I've left it the same or am I just trying to achieve the impossible?
Cheers.
4 1585
It looks to me that in line 18 you are passing the incremented pointer ( incremented at 15 ). Why do you not remove lines 14 to 16, since this was the point anyway?
Another problem. In calculateSHA1() you are returning a pointer to an automatic, which will 'go away' at some point.
It looks to me that in line 18 you are passing the incremented pointer ( incremented at 15 ). Why do you not remove lines 14 to 16, since this was the point anyway?
opps, in my actual code I had, this was a cut and paste jobby from the previous code snippet and got left in. careless
Another problem. In calculateSHA1() you are returning a pointer to an automatic, which will 'go away' at some point.
So I am and indeed it does. Spotters badge for ravenspoint, problem solved :O)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Andy Read |
last post by:
Dear all,
I thought I understood passing parameters ByVal and ByRef but I
clearly don't! If I define a simple class
of:
Public Class Person
Public Name as String
Public Age as Integer
End...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: kalinga1234 |
last post by:
there is a problem regarding passing array of characters to another
function(without using structures,pointer etc,).can anybody help me to
solve the problem.
|
by: keepyourstupidspam |
last post by:
Hi,
I want to pass a function pointer that is a class member.
This is the fn I want to pass the function pointer into:
int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
|
by: Charles Sullivan |
last post by:
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int...
| |
by: Christopher Benson-Manica |
last post by:
Does the following program exhibit undefined behavior? Specifically,
does passing a struct by value cause undefined behavior if that struct
has as a member a pointer that has been passed to...
|
by: Mike |
last post by:
Consider the following code:
"""
struct person {
char *name;
int age;
};
typedef struct person* StructType;
|
by: Roman Mashak |
last post by:
Hello,
I belive the reason of problem is simple, but can't figure out.
This is piece of code:
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
|
by: Ivan Liu |
last post by:
Hi,
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
Ivan
|
by: TS |
last post by:
I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |