/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/hash/md5sum.c

Go to the documentation of this file.
00001 /*
00002  *  md5sum demonstration program
00003  *
00004  *  Copyright (C) 2006-2007  Christophe Devine
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License, version 2.1 as published by the Free Software Foundation.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  *  MA  02110-1301  USA
00019  */
00020 
00021 #ifndef _CRT_SECURE_NO_DEPRECATE
00022 #define _CRT_SECURE_NO_DEPRECATE 1
00023 #endif
00024 
00025 #include <string.h>
00026 #include <stdio.h>
00027 
00028 #include "xyssl/md5.h"
00029 
00030 int md5_wrapper( char *filename, unsigned char *sum )
00031 {
00032     int ret = md5_file( filename, sum );
00033 
00034     if( ret == 1 )
00035         fprintf( stderr, "failed to open: %s\n", filename );
00036 
00037     if( ret == 2 )
00038         fprintf( stderr, "failed to read: %s\n", filename );
00039 
00040     return( ret );
00041 }
00042 
00043 int md5_print( char *filename )
00044 {
00045     int i;
00046     unsigned char sum[16];
00047 
00048     if( md5_wrapper( filename, sum ) != 0 )
00049         return( 1 );
00050 
00051     for( i = 0; i < 16; i++ )
00052         printf( "%02x", sum[i] );
00053 
00054     printf( "  %s\n", filename );
00055     return( 0 );
00056 }
00057 
00058 int md5_check( char *filename )
00059 {
00060     int i;
00061     size_t n;
00062     FILE *f;
00063     int nb_err1, nb_err2;
00064     int nb_tot1, nb_tot2;
00065     unsigned char sum[16];
00066     char buf[33], line[1024];
00067 
00068     if( ( f = fopen( filename, "rb" ) ) == NULL )
00069     {
00070         printf( "failed to open: %s\n", filename );
00071         return( 1 );
00072     }
00073 
00074     nb_err1 = nb_err2 = 0;
00075     nb_tot1 = nb_tot2 = 0;
00076 
00077     memset( line, 0, sizeof( line ) );
00078 
00079     n = sizeof( line );
00080 
00081     while( fgets( line, n - 1, f ) != NULL )
00082     {
00083         n = strlen( line );
00084 
00085         if( n < 36 )
00086             continue;
00087 
00088         if( line[32] != ' ' || line[33] != ' ' )
00089             continue;
00090 
00091         if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
00092         if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
00093 
00094         nb_tot1++;
00095 
00096         if( md5_wrapper( line + 34, sum ) != 0 )
00097         {
00098             nb_err1++;
00099             continue;
00100         }
00101 
00102         nb_tot2++;
00103 
00104         for( i = 0; i < 16; i++ )
00105             sprintf( buf + i * 2, "%02x", sum[i] );
00106 
00107         if( memcmp( line, buf, 32 ) != 0 )
00108         {
00109             nb_err2++;
00110             fprintf( stderr, "wrong checksum: %s\n", line + 34 );
00111         }
00112 
00113         n = sizeof( line );
00114     }
00115 
00116     if( nb_err1 != 0 )
00117     {
00118         printf( "WARNING: %d (out of %d) input files could "
00119                 "not be read\n", nb_err1, nb_tot1 );
00120     }
00121 
00122     if( nb_err2 != 0 )
00123     {
00124         printf( "WARNING: %d (out of %d) computed checksums did "
00125                 "not match\n", nb_err2, nb_tot2 );
00126     }
00127 
00128     return( nb_err1 != 0 || nb_err2 != 0 );
00129 }
00130 
00131 int main( int argc, char *argv[] )
00132 {
00133     int ret, i;
00134 
00135     if( argc == 1 )
00136     {
00137         printf( "print mode:  md5sum <file> <file> ...\n" );
00138         printf( "check mode:  md5sum -c <checksum file>\n" );
00139 
00140 #ifdef WIN32
00141         printf( "\n  Press Enter to exit this program.\n" );
00142         fflush( stdout ); getchar();
00143 #endif
00144 
00145         return( 1 );
00146     }
00147 
00148     if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
00149         return( md5_check( argv[2] ) );
00150 
00151     ret = 0;
00152     for( i = 1; i < argc; i++ )
00153         ret |= md5_print( argv[i] );
00154 
00155     return( ret );
00156 }

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4