oveh
asked on
Returning an array from a function ?
I'm playing with a hashing algorithm called MD-4,
and need to return the contents of an
'unsigned char digest[16]'
from my function, rather than the address to it.
How should i set it up ?
I'm using the global.h and the md.h files which can be found in this article:
http://theory.lcs.mit.edu/~rivest/Rivest-MD4.txt
The test code would look something like this:
#include....various
// define MD-4 specific things
#define MD 4
#define MD_CTX MD4_CTX
#define MDInit MD4Init
#define MDUpdate MD4Update
#define MDFinal MD4Final
int main (int argc, char *argv[])
{
char *teststreng;
unsigned char *string = "abc";
char *test;
teststreng = OWF (string);
test = MDPrint (teststreng);
printf("The answer is: %s \n",test);
return (0);
}
unsigned char * OWF (unsigned char *string)
{ int i,j;
MD_CTX context;
unsigned char digest[ 16 ];
char *test ;
unsigned int len = strlen (string);
// The MD-4 calls...puts the 4 blocks a 32 bits into
// digest (128 bits, 16 bytes)
MDInit (&context) ;
MDUpdate (&context, string, len);
MDFinal (digest, &context);
// MDPrint makes the digest into hexadecimals
test = MDPrint (digest);
printf("MD%d - 1 = %s\n", MD,test);
return digest;
}
// turns a unsigned char into a hexadecimal string
char *MDPrint (unsigned char *digest)
{ int i;
static char buf[64];
for (i = 0; i < 16; i++) sprintf(&(buf[i*2]),"%02x" , digest[i]);
return(buf);
}
and need to return the contents of an
'unsigned char digest[16]'
from my function, rather than the address to it.
How should i set it up ?
I'm using the global.h and the md.h files which can be found in this article:
http://theory.lcs.mit.edu/~rivest/Rivest-MD4.txt
The test code would look something like this:
#include....various
// define MD-4 specific things
#define MD 4
#define MD_CTX MD4_CTX
#define MDInit MD4Init
#define MDUpdate MD4Update
#define MDFinal MD4Final
int main (int argc, char *argv[])
{
char *teststreng;
unsigned char *string = "abc";
char *test;
teststreng = OWF (string);
test = MDPrint (teststreng);
printf("The answer is: %s \n",test);
return (0);
}
unsigned char * OWF (unsigned char *string)
{ int i,j;
MD_CTX context;
unsigned char digest[ 16 ];
char *test ;
unsigned int len = strlen (string);
// The MD-4 calls...puts the 4 blocks a 32 bits into
// digest (128 bits, 16 bytes)
MDInit (&context) ;
MDUpdate (&context, string, len);
MDFinal (digest, &context);
// MDPrint makes the digest into hexadecimals
test = MDPrint (digest);
printf("MD%d - 1 = %s\n", MD,test);
return digest;
}
// turns a unsigned char into a hexadecimal string
char *MDPrint (unsigned char *digest)
{ int i;
static char buf[64];
for (i = 0; i < 16; i++) sprintf(&(buf[i*2]),"%02x"
return(buf);
}
ASKER
So how would the code be like ?
something like :
typedef struct bit_struct{ unsigned char digest[16]; } bitstring;
bitstring * OWF ( unsigned char *)
and then what ??
I'm rather fresh at structs....
something like :
typedef struct bit_struct{ unsigned char digest[16]; } bitstring;
bitstring * OWF ( unsigned char *)
and then what ??
I'm rather fresh at structs....
bitstring OWF( unsigned char * string )
ASKER
how should I treat digest in the OWF ?
it's just a sample code...I'm primarily interested in knowing all the details needed for returning the
contents of an array from a function.
so...how does the introduction of struct into the code affect the code. What is added ? and where?
(thanks for your time by the way )
it's just a sample code...I'm primarily interested in knowing all the details needed for returning the
contents of an array from a function.
so...how does the introduction of struct into the code affect the code. What is added ? and where?
(thanks for your time by the way )
ASKER
how should I treat digest in the OWF ?
it's just a sample code...I'm primarily interested in knowing all the details needed for returning the
contents of an array from a function.
so...how does the introduction of struct into the code affect the code. What is added ? and where?
(thanks for your time by the way )
it's just a sample code...I'm primarily interested in knowing all the details needed for returning the
contents of an array from a function.
so...how does the introduction of struct into the code affect the code. What is added ? and where?
(thanks for your time by the way )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or you could pass to the function the address of an array in which to return the contents.