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