blob: 52939e4873959c80b60c6b42ac6cca8ab6b497f4 [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 Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/sha2.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker5690efc2011-05-26 13:16:06 +000037#if !defined(POLARSSL_SHA2_C) || !defined(POLARSSL_FS_IO)
38int main( void )
39{
40 printf("POLARSSL_SHA2_C and/or POLARSSL_FS_IO not defined.\n");
41 return( 0 );
42}
43#else
Paul Bakker5121ce52009-01-03 21:22:43 +000044static int sha2_wrapper( char *filename, unsigned char *sum )
45{
46 int ret = sha2_file( filename, sum, 0 );
47
48 if( ret == 1 )
49 fprintf( stderr, "failed to open: %s\n", filename );
50
51 if( ret == 2 )
52 fprintf( stderr, "failed to read: %s\n", filename );
53
54 return( ret );
55}
56
57static int sha2_print( char *filename )
58{
59 int i;
60 unsigned char sum[32];
61
62 if( sha2_wrapper( filename, sum ) != 0 )
63 return( 1 );
64
65 for( i = 0; i < 32; i++ )
66 printf( "%02x", sum[i] );
67
68 printf( " %s\n", filename );
69 return( 0 );
70}
71
72static int sha2_check( char *filename )
73{
74 int i;
75 size_t n;
76 FILE *f;
77 int nb_err1, nb_err2;
78 int nb_tot1, nb_tot2;
79 unsigned char sum[32];
80 char buf[65], line[1024];
81
82 if( ( f = fopen( filename, "rb" ) ) == NULL )
83 {
84 printf( "failed to open: %s\n", filename );
85 return( 1 );
86 }
87
88 nb_err1 = nb_err2 = 0;
89 nb_tot1 = nb_tot2 = 0;
90
91 memset( line, 0, sizeof( line ) );
92
93 n = sizeof( line );
94
Paul Bakker23986e52011-04-24 08:57:21 +000095 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000096 {
97 n = strlen( line );
98
99 if( n < 68 )
100 continue;
101
102 if( line[64] != ' ' || line[65] != ' ' )
103 continue;
104
105 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
106 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
107
108 nb_tot1++;
109
110 if( sha2_wrapper( line + 66, sum ) != 0 )
111 {
112 nb_err1++;
113 continue;
114 }
115
116 nb_tot2++;
117
118 for( i = 0; i < 32; i++ )
119 sprintf( buf + i * 2, "%02x", sum[i] );
120
121 if( memcmp( line, buf, 64 ) != 0 )
122 {
123 nb_err2++;
124 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
125 }
126
127 n = sizeof( line );
128 }
129
130 if( nb_err1 != 0 )
131 {
132 printf( "WARNING: %d (out of %d) input files could "
133 "not be read\n", nb_err1, nb_tot1 );
134 }
135
136 if( nb_err2 != 0 )
137 {
138 printf( "WARNING: %d (out of %d) computed checksums did "
139 "not match\n", nb_err2, nb_tot2 );
140 }
141
142 return( nb_err1 != 0 || nb_err2 != 0 );
143}
144
145int main( int argc, char *argv[] )
146{
147 int ret, i;
148
149 if( argc == 1 )
150 {
151 printf( "print mode: sha2sum <file> <file> ...\n" );
152 printf( "check mode: sha2sum -c <checksum file>\n" );
153
154#ifdef WIN32
155 printf( "\n Press Enter to exit this program.\n" );
156 fflush( stdout ); getchar();
157#endif
158
159 return( 1 );
160 }
161
162 if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
163 return( sha2_check( argv[2] ) );
164
165 ret = 0;
166 for( i = 1; i < argc; i++ )
167 ret |= sha2_print( argv[i] );
168
169 return( ret );
170}
Paul Bakker5690efc2011-05-26 13:16:06 +0000171#endif /* POLARSSL_SHA2_C && POLARSSL_FS_IO */