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