00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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/sha2.h"
00029
00030 static int sha2_wrapper( char *filename, unsigned char *sum )
00031 {
00032 int ret = sha2_file( filename, sum, 0 );
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 sha2_print( char *filename )
00044 {
00045 int i;
00046 unsigned char sum[32];
00047
00048 if( sha2_wrapper( filename, sum ) != 0 )
00049 return( 1 );
00050
00051 for( i = 0; i < 32; i++ )
00052 printf( "%02x", sum[i] );
00053
00054 printf( " %s\n", filename );
00055 return( 0 );
00056 }
00057
00058 static int sha2_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[32];
00066 char buf[65], 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 < 68 )
00086 continue;
00087
00088 if( line[64] != ' ' || line[65] != ' ' )
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( sha2_wrapper( line + 66, sum ) != 0 )
00097 {
00098 nb_err1++;
00099 continue;
00100 }
00101
00102 nb_tot2++;
00103
00104 for( i = 0; i < 32; i++ )
00105 sprintf( buf + i * 2, "%02x", sum[i] );
00106
00107 if( memcmp( line, buf, 64 ) != 0 )
00108 {
00109 nb_err2++;
00110 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
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: sha2sum <file> <file> ...\n" );
00138 printf( "check mode: sha2sum -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( sha2_check( argv[2] ) );
00150
00151 ret = 0;
00152 for( i = 1; i < argc; i++ )
00153 ret |= sha2_print( argv[i] );
00154
00155 return( ret );
00156 }