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);
}
ovehAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
bitstring OWF (unsigned char *string)
                { int i,j;
                 MD_CTX context;
                 bitstring contents;    
                 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  (contents.digest, &context);

                // MDPrint makes the digest into hexadecimals    
                  test = MDPrint (contents.digest);
                  printf("MD%d - 1 = %s\n", MD,test);
                   
                 return contents;
}

teststreng = OWF (string).digest;
0
 
ozoCommented:
You can return a struct containing unsigned char digest[16]
Or you could pass to the function the address of an array in which to return the contents.
0
 
ovehAuthor Commented:
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....

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
ozoCommented:
bitstring OWF( unsigned char * string )
0
 
ovehAuthor Commented:
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 )
0
 
ovehAuthor Commented:
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 )
0
All Courses

From novice to tech pro — start learning today.