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