Compare hashes in c - c

I am trying to compare and match hashes:
#include <stdio.h>
#include <string.h>
#include "sha256.h"
int main()
{
unsigned char password[]={"abc"}, gen_hash[32];
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx,password,strlen(password));
sha256_final(&ctx,gen_hash);
unsigned char orig_hash[] = {"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"};
if(strcmp(orig_hash, gen_hash) == 0)
{
printf("%s\n", "match");
}
return 0;
}
But If I compare both hashes, they are not the same. Does anyone happen to know why? I thought both the variables are the same, but are they not?

Two points:
Don't use strcmp but rather memcmp since the generated hash gen_hash won't have '\0' at the end.
You defined orig_hash as
unsigned char orig_hash[] =
{"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"};
which is a string of characters. You should define it as a array of numbers:
unsigned char orig_hash[] = {0xba, 0x78, 0x16, 0xbf, ...};

Related

How can I convert a string of binary numbers into characters in C (each character has to have a nibble)?

I already looked for useful functions that I can use to convert my string of binary numbers into a string of ASCII characters. I tried converting the string using these C++ libraries
#include <sstream>
#include <iostream>
But my programming environment (CVI from national instruments) does not have them.
I would be glad if someone has an example of a converting function!
Thanks a lot in advance.
Your question is very vague, and no actual attempt is shown which makes it even harder to figure out what you mean.
Perhaps something like this:
#include <stdio.h>
void hexprint(char *out, const void *in, size_t length)
{
const static char hexdigits[] = "0123456789abcdef";
const unsigned char *bin = in;
while (length--) {
const unsigned char here = *bin++;
*out++ = hexdigits[here >> 4];
*out++ = hexdigits[here & 15];
}
*out = '\0';
}
int main(void)
{
char buf[32], data[] = {0xde, 0xad, 0xf0, 0x0d, 0xba, 0xbe };
hexprint(buf, data, sizeof data);
printf("Got '%s'\n", buf);
return 0;
}
This prints Got 'deadf00dbabe'. Note that the output buffer is silently assumed to be Big Enough(TM), be careful.

C char of string to array

I have 2 defines, one with a string and one with a number.How can i make a const array from the define with the string and the number. There are also some additional constant which should be in this array.
How can i write this Code to have 0x22, 0x41, 0x42, 0x42, 0x21 in the array foobar, from the defines FOO and BAR?
#define FOO "AB"
#define BAR 33
extern int rs232_write(const unsigned char *data, unsigned char count);
const unsigned char foobar[] =
{
0x22,
FOO[0], /*what must i put here, this do not work*/
FOO[1],
0x42,
BAR,
};
int main(void)
{
rs232_write(foobar,sizeof(foobar));
return 1;
}
In gcc, for example, i get the error:
./001.c:9:5: error: initializer element is not constant
FOO[0], /*what must i put here*/
^
The String have always the same length.
I did also a try the other way around:
#define FOO "AB"
#define BAR 33
extern int rs232_write(const unsigned char *data, unsigned char count);
const char foobar[] = \
"\x22" \
FOO \
"\x42" \
BAR /*what must i put here, this also not work*/
int main(void)
{
rs232_write(foobar,sizeof(foobar));
return 1;
}
Here i get also a error, for example gcc prints:
./002.c:2:13: error: expected ‘,’ or ‘;’ before numeric constant
#define BAR 33
^
I working on a Microcontroller with not much space, so i would like to avoid creating the array at runtime and my compiler do only support C89.
The simplest, using memcpy:
#include <stdio.h>
#include <string.h>
#define FOO "AB"
#define BAR 33
extern int rs232_write(const unsigned char *data, unsigned char count);
unsigned char _foobar[] =
{
0x22,
0, 0,
0x42,
BAR,
};
const unsigned char *foobar;
int main(void)
{
foobar = (const unsigned char *)memcpy(_foobar + 1, FOO, 2) - 1;
rs232_write(foobar,sizeof(foobar));
return 0;
}
The ugly, using an X Macro and a compound literal:
In this way you can use the first two digits:
const unsigned char foobar[] =
{
0x22,
'A', 'B',
0x42,
33,
};
or the full string "AB"
#include <stdio.h>
#define FOO X('A', 'B', '\0')
#define BAR 33
extern int rs232_write(const unsigned char *data, unsigned char count);
const unsigned char foobar[] =
{
0x22,
#define X(a, b, c) a, b
FOO,
#undef X
#define X(a, b, c) ((char []){a, b, c})
0x42,
BAR,
};
int main(void)
{
// rs232_write(foobar,sizeof(foobar));
printf("%s\n", FOO);
return 0;
}
Output:
AB
This should work:
#include<stdio.h>
#define FOO 'A','B'
#define BAR 33
const char foobar[] = {
0x22,
FOO,
0x42,
BAR,
'\0'
};
int main(void)
{
printf("%s\n", foobar);
return 0;
}
BTW it is very bad to init the array that way, maybe you can explain your aim better.
#include <stdio.h>
#include <stdlib.h>
#define FOO "ab"
#define BAR 33
#define STRINGIFY(x) STRINGIFY2(x)
#define STRINGIFY2(x) #x
const char foobar[] = "\x22" FOO "\x42" STRINGIFY(BAR);
int main(void)
{
printf("foobar = |%s| (%ld+1 characters)\n",
foobar, (long) sizeof(foobar) - 1);
return EXIT_SUCCESS;
}
Running this program ouputs:
foobar = |"abB33| (6+1 characters)
The problem is that the compiler doesn't know, at the time of compilation, where the string literal "AB" will be placed in memory. Its location will be decided when the program is linked or possibly when it is loaded into memory.
Therefore using it will not result in a compile-time constant that is required for the initialization of the foobar array.
In this case you really have no option but to use setting foobar[1] and foobar[2] once at run-time. However, even on an extremely small embedded system this will not incur much overhead either in memory or in time. If the program runs more than a few seconds it will most likely not even be measurable.

Can't able to copy value? Is logic error? or print error?

Hi i have below simple program
#include <stdio.h>
#include <string.h>
typedef unsigned long long uint64;
void getvalue(uint64 *getValue)
{
unsigned char arr[8] = {0xAB, 0xCD, 0x12, 0x34, 0xFF, 0xED, 0xCA, 0x01};
memcpy(getValue, arr, sizeof(uint64));
}
void main()
{
uint64 getValue;
getvalue(&getValue);
printf("value :0x%08x and sizeof(uint64) = %d", getValue, sizeof(uint64));
}
This program to copy content in 8 byte variable but when i ran it i see below output which shows only 4 bytes copied.
value :0x3412cdab and sizeof(uint64) = 8
So can anyone point out me what is the issue?
The copy is ok, You have to printf with long long unsigned
printf("0x%llx", (unsigned long long) getValue);

Sending a pointer of a character array to a function in C

Okay, long story short I am having some trouble:
I'm trying to send the starting address of a character array (string) to a function. Once the function gets the pointer, I'd like the function to parse through the characters one by one (in order to modify certain characters). As of right now I just set it up to print each character, but I'm still failing at that really badly.
This is what I have:
#include "system.h"
#include <stdio.h>
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
void EXCLAIM(char *msg){
char the_char = 0;
the_char = msg;
while (the_char != 0)
{
printf(the_char);
*msg = *(msg++);
the_char = *msg;
}
}
int main(void) {
char *first_str = "This is a test. Will this work. I. am. Not. Sure...";
while (1) {
EXCLAIM(first_str);
}
}
EDIT:
Here is the updated code with what I was trying to do; send the pointer and go through each character replacing all periods with exclamation marks.
#include "system.h"
#include <stdio.h>
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
void exclaim(char *msg){
int i;
for( i=0; msg[i]; i++ )
{
if (msg[i] == '.') {
msg[i] = '!';
}
}
printf(msg);
}
int main(void) {
char *the_sentences = "This is a test. Will this work. I. am. Not. Sure...";
while (1) {
exclaim(the_sentences);
}
}
Thank you all for your help!
Your main() is fine.
The issue lies in the pointer arithmetics inside EXCLAIM.
I can see two different versions that are useful:
Using pointer arithmetics
while (*msg)
{
printf("%c", *msg);
msg++
}
Using indexing
int i;
char the_char;
for( i=0; msg[i]; i++ )
{
printf( "%c", msg[i] );
}
Try something like this:
#include <stdio.h>
void exclaim(char * s)
{
while (*s != '\0')
{
putc(*s);
++s;
}
}
Notes:
Don't shout. Don't use all caps for anything but preprocessor macros.
Print characters, not strings.
No need to make another copy. The function argument is already a local variable that you can use directly.
You should use:
the_char = *msg;
not:
the_char = msg;
*msg = *(msg++);
What are you trying to do with this line of code? Do you understand what it actually does? First off, you need to know that msg++ changes the value of the pointer. Then you assign whatever it USED to point to to the new location. This means that the whole string will be replaced with copies of the first character in the string.
The moral of the story is to not do too much in a single line of code. You have to be especially careful with the increment operator ++. Even seasoned programmers typically put something like msg++ in its own line because it gets too complicated when you try to mix it in with a complex expression.
Something that was not mentioned but, attemping to modify a string literal is undefined behavior.
You need to change the following line from,
char *the_sentences = "This is a test. Will this work. I. am. Not. Sure...";
to an array.
char the_sentences[] = "This is a test. Will this work. I. am. Not. Sure...";
Then feel free to modify the contents inside your function.
#include <stdio.h>
void foo(char *msg)
{
for (; *msg; msg++)
if (*msg == '?')
*msg = '!';
}
int main(void)
{
char line[] = "Hello, world?? How are you tonight??";
foo(line);
puts(line);
return 0;
}

Random string from an array in c

I'm kind of new to C, but not to programming. I'm trying to create a program that takes an input and replies with a random string that's already saved in an array (for example).
I'm not trying to create a random string, I want them to be "fixed", like in Java:
String [] sa;
sa[0] = "Hello, World";
sa[1] = "Hi dude!";
const char *sa[]={"Hello, World","Hi dude!"};
Then you can do
return sa[i];
The return value is char *
Just make sure i is within bounds
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *messages[] = {
"Hello!",
"How are you?",
"Good stuff!"
};
const size_t messages_count = sizeof(messages) / sizeof(messages[0]);
char input[64];
while (1) {
scanf("%63s", input);
printf("%s\n", messages[rand() % messages_count]);
}
return 0;
}
It's not clear as to what you exactly want, but here is a brief description of how strings work in C.
There are no String like data type in C as you have in Java. You have to use array of characters. For an array of strings, you have to use two dimensional array of characters.
char myStrings[MAX_NUMBER_OF_STRING][MAX_LENGTH_OF_STRING];
Here what your are looking for:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char buffer[42];
const char *mytext[] = {"A1", "A2", "A3"};
scanf("%41s", buffer);
srand(time(NULL));
printf("Random text: %s\n", mytext[rand() % (sizeof(mytext) / sizeof(mytext[0]))]);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(){
char str[7][100]={"hello1","hello2","hello3","hello4","hello5","hello6","hello7"};
printf("%s",str[rand()%7]);
return 0;
}

Resources