blob: 78ee21e8fb338b906c4cfd75a4d66b51c98990b3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * sha1sum demonstration program
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker27db1f52009-01-25 15:27:00 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
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 Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32static int sha1_wrapper( char *filename, unsigned char *sum )
33{
34 int ret = sha1_file( filename, sum );
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
45static int sha1_print( char *filename )
46{
47 int i;
48 unsigned char sum[20];
49
50 if( sha1_wrapper( filename, sum ) != 0 )
51 return( 1 );
52
53 for( i = 0; i < 20; i++ )
54 printf( "%02x", sum[i] );
55
56 printf( " %s\n", filename );
57 return( 0 );
58}
59
60static int sha1_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[20];
68 char buf[41], 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 < 44 )
88 continue;
89
90 if( line[40] != ' ' || line[41] != ' ' )
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( sha1_wrapper( line + 42, sum ) != 0 )
99 {
100 nb_err1++;
101 continue;
102 }
103
104 nb_tot2++;
105
106 for( i = 0; i < 20; i++ )
107 sprintf( buf + i * 2, "%02x", sum[i] );
108
109 if( memcmp( line, buf, 40 ) != 0 )
110 {
111 nb_err2++;
112 fprintf( stderr, "wrong checksum: %s\n", line + 42 );
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
133int main( int argc, char *argv[] )
134{
135 int ret, i;
136
137 if( argc == 1 )
138 {
139 printf( "print mode: sha1sum <file> <file> ...\n" );
140 printf( "check mode: sha1sum -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( sha1_check( argv[2] ) );
152
153 ret = 0;
154 for( i = 1; i < argc; i++ )
155 ret |= sha1_print( argv[i] );
156
157 return( ret );
158}