blob: 446d60b2ace1c0d6f9ccc6e6f3811e46059f3dfa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Paul Bakker9e36f042013-06-30 14:34:05 +02002 * sha256sum demonstration program
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Paul Bakkerd2681d82013-06-30 14:49:12 +02004 * Copyright (C) 2006-2013, 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 Bakkerd2681d82013-06-30 14:49:12 +020035#include "polarssl/sha256.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker9e36f042013-06-30 14:34:05 +020037#if !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000038int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000039{
Paul Bakkercce9d772011-11-18 14:26:47 +000040 ((void) argc);
41 ((void) argv);
42
Paul Bakker9e36f042013-06-30 14:34:05 +020043 printf("POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000044 return( 0 );
45}
46#else
Paul Bakker9e36f042013-06-30 14:34:05 +020047static int sha256_wrapper( char *filename, unsigned char *sum )
Paul Bakker5121ce52009-01-03 21:22:43 +000048{
Paul Bakker9e36f042013-06-30 14:34:05 +020049 int ret = sha256_file( filename, sum, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51 if( ret == 1 )
52 fprintf( stderr, "failed to open: %s\n", filename );
53
54 if( ret == 2 )
55 fprintf( stderr, "failed to read: %s\n", filename );
56
57 return( ret );
58}
59
Paul Bakker9e36f042013-06-30 14:34:05 +020060static int sha256_print( char *filename )
Paul Bakker5121ce52009-01-03 21:22:43 +000061{
62 int i;
63 unsigned char sum[32];
64
Paul Bakker9e36f042013-06-30 14:34:05 +020065 if( sha256_wrapper( filename, sum ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000066 return( 1 );
67
68 for( i = 0; i < 32; i++ )
69 printf( "%02x", sum[i] );
70
71 printf( " %s\n", filename );
72 return( 0 );
73}
74
Paul Bakker9e36f042013-06-30 14:34:05 +020075static int sha256_check( char *filename )
Paul Bakker5121ce52009-01-03 21:22:43 +000076{
77 int i;
78 size_t n;
79 FILE *f;
80 int nb_err1, nb_err2;
81 int nb_tot1, nb_tot2;
82 unsigned char sum[32];
83 char buf[65], line[1024];
84
85 if( ( f = fopen( filename, "rb" ) ) == NULL )
86 {
87 printf( "failed to open: %s\n", filename );
88 return( 1 );
89 }
90
91 nb_err1 = nb_err2 = 0;
92 nb_tot1 = nb_tot2 = 0;
93
94 memset( line, 0, sizeof( line ) );
95
96 n = sizeof( line );
97
Paul Bakker23986e52011-04-24 08:57:21 +000098 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000099 {
100 n = strlen( line );
101
102 if( n < 68 )
103 continue;
104
105 if( line[64] != ' ' || line[65] != ' ' )
106 continue;
107
108 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
109 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
110
111 nb_tot1++;
112
Paul Bakker9e36f042013-06-30 14:34:05 +0200113 if( sha256_wrapper( line + 66, sum ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 {
115 nb_err1++;
116 continue;
117 }
118
119 nb_tot2++;
120
121 for( i = 0; i < 32; i++ )
122 sprintf( buf + i * 2, "%02x", sum[i] );
123
124 if( memcmp( line, buf, 64 ) != 0 )
125 {
126 nb_err2++;
127 fprintf( stderr, "wrong checksum: %s\n", line + 66 );
128 }
129
130 n = sizeof( line );
131 }
132
133 if( nb_err1 != 0 )
134 {
135 printf( "WARNING: %d (out of %d) input files could "
136 "not be read\n", nb_err1, nb_tot1 );
137 }
138
139 if( nb_err2 != 0 )
140 {
141 printf( "WARNING: %d (out of %d) computed checksums did "
142 "not match\n", nb_err2, nb_tot2 );
143 }
144
145 return( nb_err1 != 0 || nb_err2 != 0 );
146}
147
148int main( int argc, char *argv[] )
149{
150 int ret, i;
151
152 if( argc == 1 )
153 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200154 printf( "print mode: sha256sum <file> <file> ...\n" );
155 printf( "check mode: sha256sum -c <checksum file>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakkercce9d772011-11-18 14:26:47 +0000157#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 printf( "\n Press Enter to exit this program.\n" );
159 fflush( stdout ); getchar();
160#endif
161
162 return( 1 );
163 }
164
165 if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
Paul Bakker9e36f042013-06-30 14:34:05 +0200166 return( sha256_check( argv[2] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 ret = 0;
169 for( i = 1; i < argc; i++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200170 ret |= sha256_print( argv[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172 return( ret );
173}
Paul Bakker9e36f042013-06-30 14:34:05 +0200174#endif /* POLARSSL_SHA256_C && POLARSSL_FS_IO */