find length of unsigned char *input?

C

cdrom205

static void MDString ( unsigned char *input)
{

MD5_CTX context;
unsigned char digest[16];
unsigned int len = sizeof(input);//strlen (const char*)

md5.MD5Init (&context);
md5.MD5Update (&context, input, len);
// void MD5Update (MD5_CTX *context, unsigned char *input, unsigned
int inputLen);
md5.MD5Final (digest, &context);

// printf ("MD5%d (\"%s\") = ", MD5, string); <= i have no idea what
is it for????
MDPrint (digest);
printf ("\n");
}
This code have bugging me for few days now. Basically, i am rewriting
and transform some of information from rfc-1321 to reusable state
(using Dev C++ - 4.9.9.2). I made mistake in this code that
"sizeof(input)" is always ==4 so the maximum characters that md5 can
disgest is 4! But no way i can get the length of "unsigned char *input"
since it is just unsigned char type not an array, if i do "static void
MDString ( unsigned char *input[16]), i willl get error.

//// Original code from the doc ///////////////
static void MDString (string)
char *string;
{
MD_CTX context;
unsigned char digest[16];
unsigned int len = strlen (string);

MDInit (&context);
MDUpdate (&context, string, len);
MDFinal (digest, &context);

printf ("MD%d (\"%s\") = ", MD, string);
MDPrint (digest);
printf ("\n");
}
I feel that i have misinterpreted some part of the code but i can't
figure it out. Can you suggest me something?(except reading a book
cause i already am)

Btw, I am still a newbie and want to improve my coding skill! Thanks!
 
R

Rolf Magnus

static void MDString ( unsigned char *input)
{

MD5_CTX context;
unsigned char digest[16];
unsigned int len = sizeof(input);//strlen (const char*)

md5.MD5Init (&context);
md5.MD5Update (&context, input, len);
// void MD5Update (MD5_CTX *context, unsigned char *input, unsigned
int inputLen);
md5.MD5Final (digest, &context);

// printf ("MD5%d (\"%s\") = ", MD5, string); <= i have no idea what
is it for????
MDPrint (digest);
printf ("\n");
}
This code have bugging me for few days now. Basically, i am rewriting
and transform some of information from rfc-1321 to reusable state
(using Dev C++ - 4.9.9.2). I made mistake in this code that
"sizeof(input)" is always ==4 so the maximum characters that md5 can
disgest is 4!

input is a pointer to unsigned char, so sizeof(input) is the size of a
pointer to unsigned char. On your platform, the size of a pointer seems to
be 4.
But no way i can get the length of "unsigned char *input"
since it is just unsigned char type not an array,

What do you mean by that? If it's just a single unsigned char, its size is
1. Or what do you mean by "not an array"? Maybe you mean it's an array, but
not zero-terminated like a C style string, so you can't use strlen()? In
this case, you have to keep track of the size yourself. Guess why MD5Update
wants an additional argument that tells it the size.
if i do "static void MDString ( unsigned char *input[16]), i willl get
error.

That would be a pointer to pointer to unsigned char.
 
D

Daniel T.

static void MDString ( unsigned char *input)
{

MD5_CTX context;
unsigned char digest[16];
unsigned int len = sizeof(input);//strlen (const char*)

md5.MD5Init (&context);
md5.MD5Update (&context, input, len);
// void MD5Update (MD5_CTX *context, unsigned char *input, unsigned
int inputLen);
md5.MD5Final (digest, &context);

// printf ("MD5%d (\"%s\") = ", MD5, string); <= i have no idea what
is it for????
MDPrint (digest);
printf ("\n");
}
This code have bugging me for few days now. Basically, i am rewriting
and transform some of information from rfc-1321 to reusable state
(using Dev C++ - 4.9.9.2). I made mistake in this code that
"sizeof(input)" is always ==4 so the maximum characters that md5 can
disgest is 4! But no way i can get the length of "unsigned char *input"
since it is just unsigned char type not an array, if i do "static void
MDString ( unsigned char *input[16]), i willl get error.

//// Original code from the doc ///////////////
static void MDString (string)
char *string;
{
MD_CTX context;
unsigned char digest[16];
unsigned int len = strlen (string);

MDInit (&context);
MDUpdate (&context, string, len);
MDFinal (digest, &context);

printf ("MD%d (\"%s\") = ", MD, string);
MDPrint (digest);
printf ("\n");
}
I feel that i have misinterpreted some part of the code but i can't
figure it out. Can you suggest me something?(except reading a book
cause i already am)

Btw, I am still a newbie and want to improve my coding skill! Thanks!

The original code assumes that the parameter passed in is a zero
terminated array of chars. Why did you change it to a non-terminated
array of unsigned chars? Fix that and everything else will fall in
place...
 
R

Rolf Magnus

Rolf said:
if i do "static void MDString ( unsigned char *input[16]), i willl get
error.

That would be a pointer to pointer to unsigned char.

Just some additional note: You can use a reference to an array:

static void MDString ( unsigned char (&input)[16])
 
C

cd_rom

First of all, thanks you guys for reply.
But no way i can get the length of "unsigned char *input"
since it is just unsigned char type not an array,
I guess, i mean that i know that code is wrong but nothing i can do
about it cause i just don't know. So i made some amendments to the
code:

static void MDString ( char *string)
{

MD5_CTX context;
unsigned char digest[16];
unsigned int len = strlen(string);//strlen (const char*)

md5.MD5Init (&context);
md5.MD5Update (&context, string, len);
// void MD5Update (MD5_CTX *context, unsigned char *input, unsigned
int inputLen);
md5.MD5Final (digest, &context);

// printf ("MD5%d (\"%s\") = ", MD5, string); <= i have no idea what
is it for????
MDPrint (digest);
printf ("\n");
}

Well, needless to say, the compiler complains about invalid type
conversion between char* and unsigned char*.

Rof Magnus: i tried change the function to static void MDString (
unsigned char (&input)[16]);but to find the size of input, i do
sizeof(input)
Here is the code:
static void MDString ( unsigned char (&input)[16])
{

//char *string;
MD5_CTX context;
unsigned char digest[16];
unsigned int len = sizeof(input);//strlen (const char*)

md5.MD5Init (&context);
md5.MD5Update (&context, input, len);
// void MD5Update (MD5_CTX *context, unsigned char *input, unsigned
int inputLen);
md5.MD5Final (digest, &context);

// printf ("MD5%d (\"%s\") = ", MD5, string); <= i have no idea what
is it for????
MDPrint (digest);
printf ("\n");
}
The code does compile and work but prints the wrong hash!
I still can't find way out of this. :~(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top