md5sum.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #ifdef PRINTF_STDLIB
  30. #include <stdio.h>
  31. #endif
  32. #ifdef PRINTF_CUSTOM
  33. #include "tinystdio.h"
  34. #endif
  35. #include "polarssl/config.h"
  36. #include "polarssl/md5.h"
  37. #if !defined(POLARSSL_MD5_C) || !defined(POLARSSL_FS_IO)
  38. int main( void )
  39. {
  40. printf("POLARSSL_MD5_C and/or POLARSSL_FS_IO not defined.\n");
  41. return( 0 );
  42. }
  43. #else
  44. static int md5_wrapper( char *filename, unsigned char *sum )
  45. {
  46. int ret = md5_file( filename, sum );
  47. if( ret == 1 )
  48. fprintf( stderr, "failed to open: %s\n", filename );
  49. if( ret == 2 )
  50. fprintf( stderr, "failed to read: %s\n", filename );
  51. return( ret );
  52. }
  53. static int md5_print( char *filename )
  54. {
  55. int i;
  56. unsigned char sum[16];
  57. if( md5_wrapper( filename, sum ) != 0 )
  58. return( 1 );
  59. for( i = 0; i < 16; i++ )
  60. printf( "%02x", sum[i] );
  61. printf( " %s\n", filename );
  62. return( 0 );
  63. }
  64. static int md5_check( char *filename )
  65. {
  66. int i;
  67. size_t n;
  68. FILE *f;
  69. int nb_err1, nb_err2;
  70. int nb_tot1, nb_tot2;
  71. unsigned char sum[16];
  72. char buf[33], line[1024];
  73. if( ( f = fopen( filename, "rb" ) ) == NULL )
  74. {
  75. printf( "failed to open: %s\n", filename );
  76. return( 1 );
  77. }
  78. nb_err1 = nb_err2 = 0;
  79. nb_tot1 = nb_tot2 = 0;
  80. memset( line, 0, sizeof( line ) );
  81. n = sizeof( line );
  82. while( fgets( line, (int) n - 1, f ) != NULL )
  83. {
  84. n = strlen( line );
  85. if( n < 36 )
  86. continue;
  87. if( line[32] != ' ' || line[33] != ' ' )
  88. continue;
  89. if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
  90. if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
  91. nb_tot1++;
  92. if( md5_wrapper( line + 34, sum ) != 0 )
  93. {
  94. nb_err1++;
  95. continue;
  96. }
  97. nb_tot2++;
  98. for( i = 0; i < 16; i++ )
  99. sprintf( buf + i * 2, "%02x", sum[i] );
  100. if( memcmp( line, buf, 32 ) != 0 )
  101. {
  102. nb_err2++;
  103. fprintf( stderr, "wrong checksum: %s\n", line + 34 );
  104. }
  105. n = sizeof( line );
  106. }
  107. if( nb_err1 != 0 )
  108. {
  109. printf( "WARNING: %d (out of %d) input files could "
  110. "not be read\n", nb_err1, nb_tot1 );
  111. }
  112. if( nb_err2 != 0 )
  113. {
  114. printf( "WARNING: %d (out of %d) computed checksums did "
  115. "not match\n", nb_err2, nb_tot2 );
  116. }
  117. return( nb_err1 != 0 || nb_err2 != 0 );
  118. }
  119. int main( int argc, char *argv[] )
  120. {
  121. int ret, i;
  122. if( argc == 1 )
  123. {
  124. printf( "print mode: md5sum <file> <file> ...\n" );
  125. printf( "check mode: md5sum -c <checksum file>\n" );
  126. #ifdef WIN32
  127. printf( "\n Press Enter to exit this program.\n" );
  128. fflush( stdout ); getchar();
  129. #endif
  130. return( 1 );
  131. }
  132. if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
  133. return( md5_check( argv[2] ) );
  134. ret = 0;
  135. for( i = 1; i < argc; i++ )
  136. ret |= md5_print( argv[i] );
  137. return( ret );
  138. }
  139. #endif /* POLARSSL_MD5_C && POLARSSL_FS_IO */