00001 /* 00002 * sha1sum demonstration program 00003 * 00004 * Copyright (C) 2006-2007 Christophe Devine 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, 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/sha1.h" 00029 00030 static int sha1_wrapper( char *filename, unsigned char *sum ) 00031 { 00032 int ret = sha1_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 static int sha1_print( char *filename ) 00044 { 00045 int i; 00046 unsigned char sum[20]; 00047 00048 if( sha1_wrapper( filename, sum ) != 0 ) 00049 return( 1 ); 00050 00051 for( i = 0; i < 20; i++ ) 00052 printf( "%02x", sum[i] ); 00053 00054 printf( " %s\n", filename ); 00055 return( 0 ); 00056 } 00057 00058 static int sha1_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[20]; 00066 char buf[41], 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 < 44 ) 00086 continue; 00087 00088 if( line[40] != ' ' || line[41] != ' ' ) 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( sha1_wrapper( line + 42, sum ) != 0 ) 00097 { 00098 nb_err1++; 00099 continue; 00100 } 00101 00102 nb_tot2++; 00103 00104 for( i = 0; i < 20; i++ ) 00105 sprintf( buf + i * 2, "%02x", sum[i] ); 00106 00107 if( memcmp( line, buf, 40 ) != 0 ) 00108 { 00109 nb_err2++; 00110 fprintf( stderr, "wrong checksum: %s\n", line + 42 ); 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: sha1sum <file> <file> ...\n" ); 00138 printf( "check mode: sha1sum -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( sha1_check( argv[2] ) ); 00150 00151 ret = 0; 00152 for( i = 1; i < argc; i++ ) 00153 ret |= sha1_print( argv[i] ); 00154 00155 return( ret ); 00156 }