Anubis13 Δημοσ. 8 Μαΐου 2013 Δημοσ. 8 Μαΐου 2013 (επεξεργασμένο) #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "md5.h" int main() { const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; unsigned char part[13]; MD5_CTX ctx; unsigned char digest[16]; int i; //srand (time(NULL)); for (i = 1; i <= 9; i++) { part[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; } for (i = 1; i <= 9; i++) { printf("Element[%d] = %c\n", i, part[i] ); } MD5Init(&ctx); MD5Update(&ctx, part, 13); MD5Final(digest, &ctx); for (i=0;i<16;i++) { printf("%0.2x", digest[i]); } putchar('\n'); getchar(); return 0; } Γιατι μου πεταει undefined references σε ολες τις md5 functions ενω αν κανω ενα char part[] = "test"; δινει κανονικά αποτέλεσμα? Επεξ/σία 8 Μαΐου 2013 από Anubis13
migf1 Δημοσ. 8 Μαΐου 2013 Δημοσ. 8 Μαΐου 2013 Τα undefined reference errors παραπέμπουν συνήθως σε συναρτήσεις που δεν έχουν γίνει linked (την κάνεις link την MD5 library)? Από την άλλη μεριά, αν δεν έχει γίνει linked η βιβλιοθήκη δεν καταλαβαίνω τι σχέση μπορεί να έχει το initialized part[] με το να σταματάνε τα undefined reference errors.
Anubis13 Δημοσ. 8 Μαΐου 2013 Μέλος Δημοσ. 8 Μαΐου 2013 Aχ ειχα κανει βλακεια στο batch file μου. Ομως και παλι δεν παιρνω σωστο αποτελεσμα. main.c: In function 'main':main.c:48:9: warning: '0' flag ignored with precision and '%x' ms_printf format [-Wformat] Tι θελει να πει ο ποιητης εδω? Κατι χανει και δινει λαθος αποτελεσμα. Kαι εχω και εξτρα λαθος. Βγαζει σωστο αποτελεσμα μονο με #define STRING "ΑBCDEF" ουτε αυτο λειτουργει char part[] = "test";
migf1 Δημοσ. 8 Μαΐου 2013 Δημοσ. 8 Μαΐου 2013 Τα %x έχουν κάποιους περιορισμούς στα formatting precision αλλά δεν θυμάμαι ακριβώς τι... δοκίμασε να το τυπώσεις με σκέτο %x.
Anubis13 Δημοσ. 8 Μαΐου 2013 Μέλος Δημοσ. 8 Μαΐου 2013 Με σκετο Χ φευγει το warning αλλα δινει και παλι λαθος αποτελεσμα. Element[1] = FElement[2] = 9Element[3] = 8Element[4] = EElement[5] = RElement[6] = 2Element[7] = 4Element[8] = SElement[9] = 84915e77896daa602f98d9a2fc7174f8 Md5 Hash: f2acf171aa89343576047ce207732b60
migf1 Δημοσ. 8 Μαΐου 2013 Δημοσ. 8 Μαΐου 2013 Μήπως στο alphanum[] πρέπει να έχεις μονάχα δεκαεξαδικά σύμβολα, δλδ 0 έως 0 και A έως F... δες τι γράφει η τεκμηρίωση των MD5 συναρτήσεων μήπως λένε τίποτα σχετικό (αν και λογικά δεν πρέπει να παίζει ρόλο). Εγώ πάω να την πέσω πάντως, καλή συνέχεια.
Star_Light Δημοσ. 8 Μαΐου 2013 Δημοσ. 8 Μαΐου 2013 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "md5.h" int main() { const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; unsigned char part[13]; MD5_CTX ctx; unsigned char digest[16]; int i; //srand (time(NULL)); for (i = 1; i <= 9; i++) { part[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; } for (i = 1; i <= 9; i++) { printf("Element[%d] = %c\n", i, part[i] ); } MD5Init(&ctx); MD5Update(&ctx, part, 13); MD5Final(digest, &ctx); for (i=0;i<16;i++) { printf("%0.2x", digest[i]); } putchar('\n'); getchar(); return 0; } Γιατι μου πεταει undefined references σε ολες τις md5 functions ενω αν κανω ενα char part[] = "test"; δινει κανονικά αποτέλεσμα? MD5 για ακεραιότητα δεδομένων???? Τα προτυπα των συναρτησεων βρισκονται στο md5.h αλλα οι ορισμοι τους σε καποιο αλλο αρχειο με .c. Δοκιμασε να μεταγλωτισεις ξεχωριστα τα αρχεια. Αν οι ορισμοι για παραδειγμα βρισκονται στο md5.c και το αρχειο που εχεις παραθεσει εδω ειναι το main.c gcc -c md5.c gcc -c main.c gcc -o md5 md5.o main.o μετα δοκιμασε να το τρέξεις απο το τερματικό.
migf1 Δημοσ. 8 Μαΐου 2013 Δημοσ. 8 Μαΐου 2013 Λοιπόν, το κύριο πρόβλημα είναι τα loops σου, που τα ξεκινάς από το 1 αντί για το 0, οπότε μένει με ότι να 'ναι τιμή ο 1ος χαρακτήρας του part[13]. Ακόμα κι αν τον έχεις initialized, το 2o for-loop σου δεν τον τυπώνει (αφού πάλι το ξεκινάς από 1 αντί για 0). Άλλαξα και κάτι casting που έδιναν warnings και βουαλα... #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define MIGF1_NAMESPACE_NO_CVS #include "md5.h" int main( void ) { unsigned char part[13] = "F98ER24S8"; MD5_CTX ctx; unsigned char digest[16]; int i; #if DISABLED const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; srand( time(NULL) ); for (i = 0; i < 9; i++) { part[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; } #endif for (i = 0; i < 9; i++) { printf("Element[%d] = %c\n", i, part[i] ); } MD5Init( &ctx ); MD5Update( &ctx, part, strlen( (char *)part) ); MD5Final( digest, &ctx ); for (i=0; i<16; i++) { printf( "%02x", (unsigned int)digest[i] ); } putchar( '\n' ); system( "pause" ); /* Windows only */ return 0; } Έξοδος: Element[0] = F Element[1] = 9 Element[2] = 8 Element[3] = E Element[4] = R Element[5] = 2 Element[6] = 4 Element[7] = S Element[8] = 8 f2acf171aa89343576047ce207732b60 Press any key to continue . . . Από τα ονόματα των MD5 συναρτήσεων υποθέτω χρησιμοποιείς κάποια νεότερη FreeBSD έκδοση της libmd, οπότε δεν χρειάζεται να ορίσεις το MIGF1_NAMESPACE_NO_CVS. Εγώ δοκίμασα με μια αρχαία "cvs_" έκδοση η οποία έχει γίνει depreciated (δίνω κώδικα που περιέχει και links, παρακάτω). Την πείραξα λιγάκι, ώστε να μπορεί να χρησιμοποιηθεί και χωρίς το "cvs_" namespace (ορίζοντας την παραπάνω σταθερά). (cvs) md5.h /* See md5.c for explanation and copyright information. */ // $FreeBSD: src/contrib/cvs/lib/md5.h,v 1.2 1999/12/11 15:10:02 peter Exp $ // http://cvsweb.xfree86.org/cvsweb/*checkout*/cvs/lib/md5.h /* ** NOTE ** * Slightly modified by <[email protected]>, removing support for the * ancient K&R style in function-parameters definition. * * Also, the default namespace "cvs_" namespace may be overcomed by defining * MIGF1_NAMESPACE_NO_CVS ** BEFORE ** including this md5.h header file in * your sources... for example, by predefining it to the compiler * (most compilers use the command-line option -D for such things). */ #ifndef MD5_H #define MD5_H /* Overcome the default namespace "cvs_" if MIGF1_NAMESPACE_NO_CVS is defined */ #ifdef MIGF1_NAMESPACE_NO_CVS typedef struct cvs_MD5Context MD5_CTX; #define MD5Init cvs_MD5Init #define MD5Update cvs_MD5Update #define MD5Final cvs_MD5Final #define MD5Transform cvs_MD5Transform #endif /* Unlike previous versions of this code, uint32 need not be exactly 32 bits, merely 32 bits or more. Choosing a data type which is 32 bits instead of 64 is not important; speed is considerably more important. ANSI guarantees that "unsigned long" will be big enough, and always using it seems to have few disadvantages. */ typedef unsigned long cvs_uint32; struct cvs_MD5Context { cvs_uint32 buf[4]; cvs_uint32 bits[2]; unsigned char in[64]; }; void cvs_MD5Init( struct cvs_MD5Context *context ); void cvs_MD5Update( struct cvs_MD5Context *context, unsigned char const *buf, unsigned len ); void cvs_MD5Final( unsigned char digest[16], struct cvs_MD5Context *context ); void cvs_MD5Transform( cvs_uint32 buf[4], const unsigned char in[64] ); #endif /* !MD5_H */ (cvs) md5.c // $FreeBSD: src/contrib/cvs/lib/md5.c // http://cvsweb.xfree86.org/cvsweb/*checkout*/cvs/lib/md5.c /* * This code implements the MD5 message-digest algorithm. * The algorithm is due to Ron Rivest. This code was * written by Colin Plumb in 1993, no copyright is claimed. * This code is in the public domain; do with it what you wish. * * Equivalent code is available from RSA Data Security, Inc. * This code has been tested against that, and is equivalent, * except that you don't need to include two pages of legalese * with every copy. * * To compute the message digest of a chunk of bytes, declare an * MD5Context structure, pass it to MD5Init, call MD5Update as * needed on buffers full of bytes, and then call MD5Final, which * will fill a supplied 16-byte array with the digest. */ /* This code was modified in 1997 by Jim Kingdon of Cyclic Software to not require an integer type which is exactly 32 bits. This work draws on the changes for the same purpose by Tatu Ylonen <[email protected]> as part of SSH, but since I didn't actually use that code, there is no copyright issue. I hereby disclaim copyright in any changes I have made; this code remains in the public domain. */ /* Note regarding cvs_* namespace: this avoids potential conflicts with libraries such as some versions of Kerberos. No particular need to worry about whether the system supplies an MD5 library, as this file is only about 3k of object code. */ /* ** NOTE by migf1 ** I have added the option to overcome the cvs_* namespace, by defining the preprocessor directive MIGF1_NAMESPACE_NO_CVS... see also the comments in the md5.h header file. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <string.h> /* for memcpy() and memset() */ #include "md5.h" /* Little-endian byte-swapping routines. Note that these do not * depend on the size of datatypes such as cvs_uint32, nor do they require * us to detect the endianness of the machine we are running on. It * is possible they should be macros for speed, but I would be * surprised if they were a performance bottleneck for MD5. */ static cvs_uint32 getu32( const unsigned char *addr ) { return (((((unsigned long)addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0]; } static void putu32( cvs_uint32 data, unsigned char *addr ) { addr[0] = (unsigned char)data; addr[1] = (unsigned char)(data >> 8); addr[2] = (unsigned char)(data >> 16); addr[3] = (unsigned char)(data >> 24); } /* * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious * initialization constants. */ void cvs_MD5Init( struct cvs_MD5Context *ctx ) { ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; ctx->buf[2] = 0x98badcfe; ctx->buf[3] = 0x10325476; ctx->bits[0] = 0; ctx->bits[1] = 0; } /************************************************************//** * Update context to reflect the concatenation of another buffer full * of bytes. */ void cvs_MD5Update( struct cvs_MD5Context *ctx, unsigned char const *buf, unsigned len ) { cvs_uint32 t; /* Update bitcount */ t = ctx->bits[0]; if ((ctx->bits[0] = (t + ((cvs_uint32)len << 3)) & 0xffffffff) < t) ctx->bits[1]++; /* Carry from low to high */ ctx->bits[1] += len >> 29; t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ /* Handle any leading odd-sized chunks */ if ( t ) { unsigned char *p = ctx->in + t; t = 64-t; if (len < t) { memcpy(p, buf, len); return; } memcpy(p, buf, t); cvs_MD5Transform (ctx->buf, ctx->in); buf += t; len -= t; } /* Process data in 64-byte chunks */ while (len >= 64) { memcpy(ctx->in, buf, 64); cvs_MD5Transform (ctx->buf, ctx->in); buf += 64; len -= 64; } /* Handle any remaining bytes of data. */ memcpy(ctx->in, buf, len); } /************************************************************//** * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ void cvs_MD5Final( unsigned char digest[16], struct cvs_MD5Context *ctx ) { unsigned count; unsigned char *p; /* Compute number of bytes mod 64 */ count = (ctx->bits[0] >> 3) & 0x3F; /* Set the first char of padding to 0x80. This is safe since there is always at least one byte free */ p = ctx->in + count; *p++ = 0x80; /* Bytes of padding needed to make 64 bytes */ count = 64 - 1 - count; /* Pad out to 56 mod 64 */ if (count < 8) { /* Two lots of padding: Pad the first block to 64 bytes */ memset(p, 0, count); cvs_MD5Transform (ctx->buf, ctx->in); /* Now fill the next block with 56 bytes */ memset(ctx->in, 0, 56); } else { /* Pad block to 56 bytes */ memset(p, 0, count-8); } /* Append length in bits and transform */ putu32(ctx->bits[0], ctx->in + 56); putu32(ctx->bits[1], ctx->in + 60); cvs_MD5Transform (ctx->buf, ctx->in); putu32(ctx->buf[0], digest); putu32(ctx->buf[1], digest + 4); putu32(ctx->buf[2], digest + 8); putu32(ctx->buf[3], digest + 12); memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ } #ifndef ASM_MD5 /* The four core functions - F1 is optimized somewhat */ /* #define F1(x, y, z) (x & y | ~x & z) */ #define F1(x, y, z) (z ^ (x & (y ^ z))) #define F2(x, y, z) F1(z, x, y) #define F3(x, y, z) (x ^ y ^ z) #define F4(x, y, z) (y ^ (x | ~z)) /* This is the central step in the MD5 algorithm. */ #define MD5STEP(f, w, x, y, z, data, s) \ ( w += f(x, y, z) + data, w &= 0xffffffff, w = w<<s | w>>(32-s), w += x ) /* * The core of the MD5 algorithm, this alters an existing MD5 hash to * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. */ void cvs_MD5Transform( cvs_uint32 buf[4], const unsigned char inraw[64] ) { register cvs_uint32 a, b, c, d; cvs_uint32 in[16]; int i; for (i = 0; i < 16; ++i) in[i] = getu32 (inraw + 4 * i); a = buf[0]; b = buf[1]; c = buf[2]; d = buf[3]; MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); buf[0] += a; buf[1] += b; buf[2] += c; buf[3] += d; } #endif #ifdef TEST /* Simple test program. Can use it to manually run the tests from RFC1321 for example. */ #include <stdio.h> #include <stdlib.h> int main (int argc, char **argv) { struct cvs_MD5Context context; unsigned char checksum[16]; int i; int j; if (argc < 2) { fprintf( stderr, "usage: %s string-to-hash\n", argv[0] ); system( "pause" ); exit(1); } for (j = 1; j < argc; ++j) { printf( "MD5 (\"%s\") = ", argv[j] ); cvs_MD5Init (&context); cvs_MD5Update( &context, argv[j], strlen(argv[j]) ); cvs_MD5Final( checksum, &context ); for (i = 0; i < 16; i++) { printf( "%02x", (unsigned int) checksum[i] ); } printf("\n"); } system( "pause" ); /* Windows only */ return 0; } #endif /* TEST */ ΥΓ. Αν αυτό το md5.c γίνει compile με -DTEST τότε τρέχει σαν μικρό δοκιμαστικό πρόγραμμα που περιμένει ένα string σαν command-line argument και το τυπώνει κωδικοποιημένο. > md5 F98ER24S8 MD5 ("F98ER24S8") = f2acf171aa89343576047ce207732b60 Press any key to continue . . . @Starlight: Μπορoύν να μπουν και όλα στην ίδια γραμμή... gcc md5_test.c md5.c -o md5_test.exe
Anubis13 Δημοσ. 8 Μαΐου 2013 Μέλος Δημοσ. 8 Μαΐου 2013 @migf1: Καταρχας σε ευχαριστω. Εφτασα και εγω στο ιδιο συμπερασμα πριν μερικα λεπτα αλλα δεν μπορουσα να φανταστω οτι εφταιγε το i=1 ημουν σιγουρος ομως οτι εφταιγε το loop
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα