473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

find length of unsigned char *input?

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!

Apr 5 '06 #1
4 14940
cd******@googlemail.com wrote:
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.

Apr 5 '06 #2
In article <11**********************@j33g2000cwa.googlegroups .com>,
"cd******@googlemail.com" <cd******@googlemail.com> wrote:
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...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 5 '06 #3
Rolf Magnus wrote:
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])

Apr 5 '06 #4
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. :~(

Apr 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Lambacher | last post by:
Hi, I have to do some data manipulation that needs to be fast. I had a generator approach (that was faster than a list approch) which was taking approximately 5 seconds to run both encode and...
6
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
18
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that...
10
by: Angus Comber | last post by:
Hello My code below opens a Word document in binary mode and places the data into a buffer. I then want to search this buffer for a string. I tried using strstr but think it stops looking when...
4
by: Amit Kulkarni | last post by:
Hi, I have small problem. I want to truncate a line in a text file using C file handling functions and write new line in place of it. How do I do it? e.g. "example.txt" Line 1: This is a...
8
by: FabioAng | last post by:
Assuming I have this function (it's not complete): template<typename InputType, typename OutputIterator> void to_utf8(InputType input, OutputIterator result) { // trivial conversion if (input...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
16
by: vizzz | last post by:
Hi there, i need to find an hex pattern like 0x650A1010 in a binary file. i can make a small algorithm that fetch all the file for the match, but this file is huge, and i'm scared about...
19
by: Eugeny Myunster | last post by:
I know, only simple one: #include <stdio.h> int main() { int min=0,max=0,i,arr; for(i=0;i<12;i++) arr=rand()%31-10; for(i=0;i<12;i++)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.