md5sum.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * md5sum demonstration program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "polarssl/config.h"
  31. #include "polarssl/md5.h"
  32. #if !defined(POLARSSL_MD5_C) || !defined(POLARSSL_FS_IO)
  33. int main( void )
  34. {
  35. printf("POLARSSL_MD5_C and/or POLARSSL_FS_IO not defined.\n");
  36. return( 0 );
  37. }
  38. #else
  39. static int md5_wrapper( char *filename, unsigned char *sum )
  40. {
  41. int ret = md5_file( filename, sum );
  42. if( ret == 1 )
  43. fprintf( stderr, "failed to open: %s\n", filename );
  44. if( ret == 2 )
  45. fprintf( stderr, "failed to read: %s\n", filename );
  46. return( ret );
  47. }
  48. static int md5_print( char *filename )
  49. {
  50. int i;
  51. unsigned char sum[16];
  52. if( md5_wrapper( filename, sum ) != 0 )
  53. return( 1 );
  54. for( i = 0; i < 16; i++ )
  55. printf( "%02x", sum[i] );
  56. printf( " %s\n", filename );
  57. return( 0 );
  58. }
  59. static int md5_check( char *filename )
  60. {
  61. int i;
  62. size_t n;
  63. FILE *f;
  64. int nb_err1, nb_err2;
  65. int nb_tot1, nb_tot2;
  66. unsigned char sum[16];
  67. char buf[33], line[1024];
  68. if( ( f = fopen( filename, "rb" ) ) == NULL )
  69. {
  70. printf( "failed to open: %s\n", filename );
  71. return( 1 );
  72. }
  73. nb_err1 = nb_err2 = 0;
  74. nb_tot1 = nb_tot2 = 0;
  75. memset( line, 0, sizeof( line ) );
  76. n = sizeof( line );
  77. while( fgets( line, (int) n - 1, f ) != NULL )
  78. {
  79. n = strlen( line );
  80. if( n < 36 )
  81. continue;
  82. if( line[32] != ' ' || line[33] != ' ' )
  83. continue;
  84. if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
  85. if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
  86. nb_tot1++;
  87. if( md5_wrapper( line + 34, sum ) != 0 )
  88. {
  89. nb_err1++;
  90. continue;
  91. }
  92. nb_tot2++;
  93. for( i = 0; i < 16; i++ )
  94. sprintf( buf + i * 2, "%02x", sum[i] );
  95. if( memcmp( line, buf, 32 ) != 0 )
  96. {
  97. nb_err2++;
  98. fprintf( stderr, "wrong checksum: %s\n", line + 34 );
  99. }
  100. n = sizeof( line );
  101. }
  102. if( nb_err1 != 0 )
  103. {
  104. printf( "WARNING: %d (out of %d) input files could "
  105. "not be read\n", nb_err1, nb_tot1 );
  106. }
  107. if( nb_err2 != 0 )
  108. {
  109. printf( "WARNING: %d (out of %d) computed checksums did "
  110. "not match\n", nb_err2, nb_tot2 );
  111. }
  112. return( nb_err1 != 0 || nb_err2 != 0 );
  113. }
  114. int main( int argc, char *argv[] )
  115. {
  116. int ret, i;
  117. if( argc == 1 )
  118. {
  119. printf( "print mode: md5sum <file> <file> ...\n" );
  120. printf( "check mode: md5sum -c <checksum file>\n" );
  121. #ifdef WIN32
  122. printf( "\n Press Enter to exit this program.\n" );
  123. fflush( stdout ); getchar();
  124. #endif
  125. return( 1 );
  126. }
  127. if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
  128. return( md5_check( argv[2] ) );
  129. ret = 0;
  130. for( i = 1; i < argc; i++ )
  131. ret |= md5_print( argv[i] );
  132. return( ret );
  133. }
  134. #endif /* POLARSSL_MD5_C && POLARSSL_FS_IO */