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