aboutsummaryrefslogtreecommitdiff
path: root/programs/hash
diff options
context:
space:
mode:
authorPaul Bakker <p.j.bakker@polarssl.org>2009-01-03 21:22:43 +0000
committerPaul Bakker <p.j.bakker@polarssl.org>2009-01-03 21:22:43 +0000
commit5121ce5bdbc92e134a42220a1aa23f95037c7625 (patch)
tree002d3c64cf627bb22e4d09c239ed1bcc3dcd2b4c /programs/hash
downloadmbedtls-5121ce5bdbc92e134a42220a1aa23f95037c7625.zip
mbedtls-5121ce5bdbc92e134a42220a1aa23f95037c7625.tar.gz
mbedtls-5121ce5bdbc92e134a42220a1aa23f95037c7625.tar.bz2
- Renamed include directory to polarssl
Diffstat (limited to 'programs/hash')
-rw-r--r--programs/hash/hello.c50
-rw-r--r--programs/hash/md5sum.c156
-rw-r--r--programs/hash/sha1sum.c156
-rw-r--r--programs/hash/sha2sum.c156
4 files changed, 518 insertions, 0 deletions
diff --git a/programs/hash/hello.c b/programs/hash/hello.c
new file mode 100644
index 0000000..12e0167
--- /dev/null
+++ b/programs/hash/hello.c
@@ -0,0 +1,50 @@
+/*
+ * Classic "Hello, world" demonstration program
+ *
+ * Copyright (C) 2006-2007 Christophe Devine
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _CRT_SECURE_NO_DEPRECATE
+#define _CRT_SECURE_NO_DEPRECATE 1
+#endif
+
+#include <stdio.h>
+
+#include "xyssl/md5.h"
+
+int main( void )
+{
+ int i;
+ unsigned char digest[16];
+ char str[] = "Hello, world!";
+
+ printf( "\n MD5('%s') = ", str );
+
+ md5( (unsigned char *) str, 13, digest );
+
+ for( i = 0; i < 16; i++ )
+ printf( "%02x", digest[i] );
+
+ printf( "\n\n" );
+
+#ifdef WIN32
+ printf( " Press Enter to exit this program.\n" );
+ fflush( stdout ); getchar();
+#endif
+
+ return( 0 );
+}
diff --git a/programs/hash/md5sum.c b/programs/hash/md5sum.c
new file mode 100644
index 0000000..10e9a71
--- /dev/null
+++ b/programs/hash/md5sum.c
@@ -0,0 +1,156 @@
+/*
+ * md5sum demonstration program
+ *
+ * Copyright (C) 2006-2007 Christophe Devine
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _CRT_SECURE_NO_DEPRECATE
+#define _CRT_SECURE_NO_DEPRECATE 1
+#endif
+
+#include <string.h>
+#include <stdio.h>
+
+#include "xyssl/md5.h"
+
+static int md5_wrapper( char *filename, unsigned char *sum )
+{
+ int ret = md5_file( filename, sum );
+
+ if( ret == 1 )
+ fprintf( stderr, "failed to open: %s\n", filename );
+
+ if( ret == 2 )
+ fprintf( stderr, "failed to read: %s\n", filename );
+
+ return( ret );
+}
+
+static int md5_print( char *filename )
+{
+ int i;
+ unsigned char sum[16];
+
+ if( md5_wrapper( filename, sum ) != 0 )
+ return( 1 );
+
+ for( i = 0; i < 16; i++ )
+ printf( "%02x", sum[i] );
+
+ printf( " %s\n", filename );
+ return( 0 );
+}
+
+static int md5_check( char *filename )
+{
+ int i;
+ size_t n;
+ FILE *f;
+ int nb_err1, nb_err2;
+ int nb_tot1, nb_tot2;
+ unsigned char sum[16];
+ char buf[33], line[1024];
+
+ if( ( f = fopen( filename, "rb" ) ) == NULL )
+ {
+ printf( "failed to open: %s\n", filename );
+ return( 1 );
+ }
+
+ nb_err1 = nb_err2 = 0;
+ nb_tot1 = nb_tot2 = 0;
+
+ memset( line, 0, sizeof( line ) );
+
+ n = sizeof( line );
+
+ while( fgets( line, n - 1, f ) != NULL )
+ {
+ n = strlen( line );
+
+ if( n < 36 )
+ continue;
+
+ if( line[32] != ' ' || line[33] != ' ' )
+ continue;
+
+ if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
+ if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
+
+ nb_tot1++;
+
+ if( md5_wrapper( line + 34, sum ) != 0 )
+ {
+ nb_err1++;
+ continue;
+ }
+
+ nb_tot2++;
+
+ for( i = 0; i < 16; i++ )
+ sprintf( buf + i * 2, "%02x", sum[i] );
+
+ if( memcmp( line, buf, 32 ) != 0 )
+ {
+ nb_err2++;
+ fprintf( stderr, "wrong checksum: %s\n", line + 34 );
+ }
+
+ n = sizeof( line );
+ }
+
+ if( nb_err1 != 0 )
+ {
+ printf( "WARNING: %d (out of %d) input files could "
+ "not be read\n", nb_err1, nb_tot1 );
+ }
+
+ if( nb_err2 != 0 )
+ {
+ printf( "WARNING: %d (out of %d) computed checksums did "
+ "not match\n", nb_err2, nb_tot2 );
+ }
+
+ return( nb_err1 != 0 || nb_err2 != 0 );
+}
+
+int main( int argc, char *argv[] )
+{
+ int ret, i;
+
+ if( argc == 1 )
+ {
+ printf( "print mode: md5sum <file> <file> ...\n" );
+ printf( "check mode: md5sum -c <checksum file>\n" );
+
+#ifdef WIN32
+ printf( "\n Press Enter to exit this program.\n" );
+ fflush( stdout ); getchar();
+#endif
+
+ return( 1 );
+ }
+
+ if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
+ return( md5_check( argv[2] ) );
+
+ ret = 0;
+ for( i = 1; i < argc; i++ )
+ ret |= md5_print( argv[i] );
+
+ return( ret );
+}
diff --git a/programs/hash/sha1sum.c b/programs/hash/sha1sum.c
new file mode 100644
index 0000000..33c3142
--- /dev/null
+++ b/programs/hash/sha1sum.c
@@ -0,0 +1,156 @@
+/*
+ * sha1sum demonstration program
+ *
+ * Copyright (C) 2006-2007 Christophe Devine
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _CRT_SECURE_NO_DEPRECATE
+#define _CRT_SECURE_NO_DEPRECATE 1
+#endif
+
+#include <string.h>
+#include <stdio.h>
+
+#include "xyssl/sha1.h"
+
+static int sha1_wrapper( char *filename, unsigned char *sum )
+{
+ int ret = sha1_file( filename, sum );
+
+ if( ret == 1 )
+ fprintf( stderr, "failed to open: %s\n", filename );
+
+ if( ret == 2 )
+ fprintf( stderr, "failed to read: %s\n", filename );
+
+ return( ret );
+}
+
+static int sha1_print( char *filename )
+{
+ int i;
+ unsigned char sum[20];
+
+ if( sha1_wrapper( filename, sum ) != 0 )
+ return( 1 );
+
+ for( i = 0; i < 20; i++ )
+ printf( "%02x", sum[i] );
+
+ printf( " %s\n", filename );
+ return( 0 );
+}
+
+static int sha1_check( char *filename )
+{
+ int i;
+ size_t n;
+ FILE *f;
+ int nb_err1, nb_err2;
+ int nb_tot1, nb_tot2;
+ unsigned char sum[20];
+ char buf[41], line[1024];
+
+ if( ( f = fopen( filename, "rb" ) ) == NULL )
+ {
+ printf( "failed to open: %s\n", filename );
+ return( 1 );
+ }
+
+ nb_err1 = nb_err2 = 0;
+ nb_tot1 = nb_tot2 = 0;
+
+ memset( line, 0, sizeof( line ) );
+
+ n = sizeof( line );
+
+ while( fgets( line, n - 1, f ) != NULL )
+ {
+ n = strlen( line );
+
+ if( n < 44 )
+ continue;
+
+ if( line[40] != ' ' || line[41] != ' ' )
+ continue;
+
+ if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
+ if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
+
+ nb_tot1++;
+
+ if( sha1_wrapper( line + 42, sum ) != 0 )
+ {
+ nb_err1++;
+ continue;
+ }
+
+ nb_tot2++;
+
+ for( i = 0; i < 20; i++ )
+ sprintf( buf + i * 2, "%02x", sum[i] );
+
+ if( memcmp( line, buf, 40 ) != 0 )
+ {
+ nb_err2++;
+ fprintf( stderr, "wrong checksum: %s\n", line + 42 );
+ }
+
+ n = sizeof( line );
+ }
+
+ if( nb_err1 != 0 )
+ {
+ printf( "WARNING: %d (out of %d) input files could "
+ "not be read\n", nb_err1, nb_tot1 );
+ }
+
+ if( nb_err2 != 0 )
+ {
+ printf( "WARNING: %d (out of %d) computed checksums did "
+ "not match\n", nb_err2, nb_tot2 );
+ }
+
+ return( nb_err1 != 0 || nb_err2 != 0 );
+}
+
+int main( int argc, char *argv[] )
+{
+ int ret, i;
+
+ if( argc == 1 )
+ {
+ printf( "print mode: sha1sum <file> <file> ...\n" );
+ printf( "check mode: sha1sum -c <checksum file>\n" );
+
+#ifdef WIN32
+ printf( "\n Press Enter to exit this program.\n" );
+ fflush( stdout ); getchar();
+#endif
+
+ return( 1 );
+ }
+
+ if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
+ return( sha1_check( argv[2] ) );
+
+ ret = 0;
+ for( i = 1; i < argc; i++ )
+ ret |= sha1_print( argv[i] );
+
+ return( ret );
+}
diff --git a/programs/hash/sha2sum.c b/programs/hash/sha2sum.c
new file mode 100644
index 0000000..c0d42a8
--- /dev/null
+++ b/programs/hash/sha2sum.c
@@ -0,0 +1,156 @@
+/*
+ * sha2sum demonstration program
+ *
+ * Copyright (C) 2006-2007 Christophe Devine
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _CRT_SECURE_NO_DEPRECATE
+#define _CRT_SECURE_NO_DEPRECATE 1
+#endif
+
+#include <string.h>
+#include <stdio.h>
+
+#include "xyssl/sha2.h"
+
+static int sha2_wrapper( char *filename, unsigned char *sum )
+{
+ int ret = sha2_file( filename, sum, 0 );
+
+ if( ret == 1 )
+ fprintf( stderr, "failed to open: %s\n", filename );
+
+ if( ret == 2 )
+ fprintf( stderr, "failed to read: %s\n", filename );
+
+ return( ret );
+}
+
+static int sha2_print( char *filename )
+{
+ int i;
+ unsigned char sum[32];
+
+ if( sha2_wrapper( filename, sum ) != 0 )
+ return( 1 );
+
+ for( i = 0; i < 32; i++ )
+ printf( "%02x", sum[i] );
+
+ printf( " %s\n", filename );
+ return( 0 );
+}
+
+static int sha2_check( char *filename )
+{
+ int i;
+ size_t n;
+ FILE *f;
+ int nb_err1, nb_err2;
+ int nb_tot1, nb_tot2;
+ unsigned char sum[32];
+ char buf[65], line[1024];
+
+ if( ( f = fopen( filename, "rb" ) ) == NULL )
+ {
+ printf( "failed to open: %s\n", filename );
+ return( 1 );
+ }
+
+ nb_err1 = nb_err2 = 0;
+ nb_tot1 = nb_tot2 = 0;
+
+ memset( line, 0, sizeof( line ) );
+
+ n = sizeof( line );
+
+ while( fgets( line, n - 1, f ) != NULL )
+ {
+ n = strlen( line );
+
+ if( n < 68 )
+ continue;
+
+ if( line[64] != ' ' || line[65] != ' ' )
+ continue;
+
+ if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
+ if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
+
+ nb_tot1++;
+
+ if( sha2_wrapper( line + 66, sum ) != 0 )
+ {
+ nb_err1++;
+ continue;
+ }
+
+ nb_tot2++;
+
+ for( i = 0; i < 32; i++ )
+ sprintf( buf + i * 2, "%02x", sum[i] );
+
+ if( memcmp( line, buf, 64 ) != 0 )
+ {
+ nb_err2++;
+ fprintf( stderr, "wrong checksum: %s\n", line + 66 );
+ }
+
+ n = sizeof( line );
+ }
+
+ if( nb_err1 != 0 )
+ {
+ printf( "WARNING: %d (out of %d) input files could "
+ "not be read\n", nb_err1, nb_tot1 );
+ }
+
+ if( nb_err2 != 0 )
+ {
+ printf( "WARNING: %d (out of %d) computed checksums did "
+ "not match\n", nb_err2, nb_tot2 );
+ }
+
+ return( nb_err1 != 0 || nb_err2 != 0 );
+}
+
+int main( int argc, char *argv[] )
+{
+ int ret, i;
+
+ if( argc == 1 )
+ {
+ printf( "print mode: sha2sum <file> <file> ...\n" );
+ printf( "check mode: sha2sum -c <checksum file>\n" );
+
+#ifdef WIN32
+ printf( "\n Press Enter to exit this program.\n" );
+ fflush( stdout ); getchar();
+#endif
+
+ return( 1 );
+ }
+
+ if( argc == 3 && strcmp( "-c", argv[1] ) == 0 )
+ return( sha2_check( argv[2] ) );
+
+ ret = 0;
+ for( i = 1; i < argc; i++ )
+ ret |= sha2_print( argv[i] );
+
+ return( ret );
+}