00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _CRT_SECURE_NO_DEPRECATE
00025 #define _CRT_SECURE_NO_DEPRECATE 1
00026 #endif
00027
00028 #include <string.h>
00029 #include <stdio.h>
00030
00031 #include "xyssl/sha2.h"
00032
00033 int sha2_wrapper( char *filename, unsigned char *sum )
00034 {
00035 int ret = sha2_file( filename, sum, 0 );
00036
00037 if( ret == 1 )
00038 fprintf( stderr, "failed to open: %s\n", filename );
00039
00040 if( ret == 2 )
00041 fprintf( stderr, "failed to read: %s\n", filename );
00042
00043 return( ret );
00044 }
00045
00046 int sha2_print( char *filename )
00047 {
00048 int i;
00049 unsigned char sum[32];
00050
00051 if( sha2_wrapper( filename, sum ) != 0 )
00052 return( 1 );
00053
00054 for( i = 0; i < 32; i++ )
00055 printf( "%02x", sum[i] );
00056
00057 printf( " %s\n", filename );
00058 return( 0 );
00059 }
00060
00061 int sha2_check( char *filename )
00062 {
00063 int i;
00064 size_t n;
00065 FILE *f;
00066 int nb_err1, nb_err2;
00067 int nb_tot1, nb_tot2;
00068 unsigned char sum[32];
00069 char buf[65], line[1024];
00070
00071 if( ( f = fopen( filename, "rb" ) ) == NULL )
00072 {
00073 printf( "failed to open: %s\n", filename );
00074 return( 1 );
00075 }
00076
00077 nb_err1 = nb_err2 = 0;
00078 nb_tot1 = nb_tot2 = 0;
00079
00080 memset( line, 0, sizeof( line ) );
00081
00082 n = sizeof( line );
00083
00084 while( fgets( line, n - 1, f ) != NULL )
00085 {
00086 n = strlen( line );
00087
00088 if( n < 68 )
00089 continue;
00090
00091 if( line[64] != ' ' || line[65] != ' ' )
00092 continue;
00093
00094 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
00095 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
00096
00097 nb_tot1++;
00098
00099 if( sha2_wrapper( line + 66, sum ) != 0 )
00100 {
00101 nb_err1++;
00102 continue;
00103 }
00104
00105 nb_tot2++;
00106
00107 for( i = 0; i < 32; i++ )
00108 sprintf( buf + i * 2, "%02x", sum[i] );
00109
00110 if( memcmp( line, buf, 64 ) != 0 )
00111 {
00112 nb_err2++;
00113 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
00114 }
00115
00116 n = sizeof( line );
00117 }
00118
00119 if( nb_err1 != 0 )
00120 {
00121 printf( "WARNING: %d (out of %d) input files could "
00122 "not be read\n", nb_err1, nb_tot1 );
00123 }
00124
00125 if( nb_err2 != 0 )
00126 {
00127 printf( "WARNING: %d (out of %d) computed checksums did "
00128 "not match\n", nb_err2, nb_tot2 );
00129 }
00130
00131 return( nb_err1 != 0 || nb_err2 != 0 );
00132 }
00133
00134 int main( int argc, char *argv[] )
00135 {
00136 int ret, i;
00137
00138 if( argc == 1 )
00139 {
00140 printf( "print mode: sha2sum <file> <file> ...\n" );
00141 printf( "check mode: sha2sum -c <checksum file>\n" );
00142
00143 #ifdef WIN32
00144 printf( "\n Press Enter to exit this program.\n" );
00145 fflush( stdout ); getchar();
00146 #endif
00147
00148 return( 1 );
00149 }
00150
00151 if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
00152 return( sha2_check( argv[2] ) );
00153
00154 ret = 0;
00155 for( i = 1; i < argc; i++ )
00156 ret |= sha2_print( argv[i] );
00157
00158 return( ret );
00159 }