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