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