Home | History | Annotate | Line # | Download | only in hash
      1  1.1  pgoyette /*	$NetBSD: h_hash.c,v 1.1 2011/01/02 22:03:25 pgoyette Exp $	*/
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.1  pgoyette  * modification, are permitted provided that the following conditions
      9  1.1  pgoyette  * are met:
     10  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.1  pgoyette  *
     16  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  pgoyette  */
     28  1.1  pgoyette 
     29  1.1  pgoyette /*
     30  1.1  pgoyette  * Combined MD5/SHA1 time and regression test.
     31  1.1  pgoyette  */
     32  1.1  pgoyette 
     33  1.1  pgoyette #include <stdio.h>
     34  1.1  pgoyette #include <stdlib.h>
     35  1.1  pgoyette #include <unistd.h>
     36  1.1  pgoyette #include <string.h>
     37  1.1  pgoyette #include <md5.h>
     38  1.1  pgoyette #include <sha1.h>
     39  1.1  pgoyette 
     40  1.1  pgoyette 
     41  1.1  pgoyette int mflag, rflag, sflag, tflag;
     42  1.1  pgoyette 
     43  1.1  pgoyette static void
     44  1.1  pgoyette usage(void)
     45  1.1  pgoyette {
     46  1.1  pgoyette 	(void)fprintf(stderr,
     47  1.1  pgoyette 	    "Usage:\t%s -r[ms] < test-file\n"
     48  1.1  pgoyette 	    "\t%s -t[ms]\n",
     49  1.1  pgoyette 	    getprogname(), getprogname());
     50  1.1  pgoyette 	exit(1);
     51  1.1  pgoyette 	/* NOTREACHED */
     52  1.1  pgoyette }
     53  1.1  pgoyette 
     54  1.1  pgoyette static void
     55  1.1  pgoyette hexdump (unsigned char *buf, int len)
     56  1.1  pgoyette {
     57  1.1  pgoyette 	int i;
     58  1.1  pgoyette 	for (i=0; i<len; i++) {
     59  1.1  pgoyette 		printf("%02x", buf[i]);
     60  1.1  pgoyette 	}
     61  1.1  pgoyette 	printf("\n");
     62  1.1  pgoyette }
     63  1.1  pgoyette 
     64  1.1  pgoyette 
     65  1.1  pgoyette static void
     66  1.1  pgoyette timetest(void)
     67  1.1  pgoyette {
     68  1.1  pgoyette 	printf("sorry, not yet\n");
     69  1.1  pgoyette }
     70  1.1  pgoyette 
     71  1.1  pgoyette #define CHOMP(buf, len, last) 				\
     72  1.1  pgoyette 	if ((len > 0) &&				\
     73  1.1  pgoyette 	    (buf[len-1] == '\n')) {			\
     74  1.1  pgoyette 		buf[len-1] = '\0';			\
     75  1.1  pgoyette 		len--;					\
     76  1.1  pgoyette 		last = 1;				\
     77  1.1  pgoyette 	}
     78  1.1  pgoyette 
     79  1.1  pgoyette static void
     80  1.1  pgoyette regress(void)
     81  1.1  pgoyette {
     82  1.1  pgoyette 	unsigned char buf[1024];
     83  1.1  pgoyette 	unsigned char out[20];
     84  1.1  pgoyette 	int len, outlen, last;
     85  1.1  pgoyette 
     86  1.1  pgoyette 	while (fgets((char *)buf, sizeof(buf), stdin) != NULL) {
     87  1.1  pgoyette 		last = 0;
     88  1.1  pgoyette 
     89  1.1  pgoyette 		len = strlen((char *)buf);
     90  1.1  pgoyette 		CHOMP(buf, len, last);
     91  1.1  pgoyette 		if (mflag) {
     92  1.1  pgoyette 			MD5_CTX ctx;
     93  1.1  pgoyette 
     94  1.1  pgoyette 			MD5Init(&ctx);
     95  1.1  pgoyette 			MD5Update(&ctx, buf, len);
     96  1.1  pgoyette 			while (!last &&
     97  1.1  pgoyette 			    fgets((char *)buf, sizeof(buf), stdin) != NULL) {
     98  1.1  pgoyette 				len = strlen((char *)buf);
     99  1.1  pgoyette 				CHOMP(buf, len, last);
    100  1.1  pgoyette 				MD5Update(&ctx, buf, len);
    101  1.1  pgoyette 			}
    102  1.1  pgoyette 			MD5Final(out, &ctx);
    103  1.1  pgoyette 			outlen = 16;
    104  1.1  pgoyette 		} else {
    105  1.1  pgoyette 			SHA1_CTX ctx;
    106  1.1  pgoyette 
    107  1.1  pgoyette 			SHA1Init(&ctx);
    108  1.1  pgoyette 			SHA1Update(&ctx, buf, len);
    109  1.1  pgoyette 			while (!last &&
    110  1.1  pgoyette 			    fgets((char *)buf, sizeof(buf), stdin) != NULL) {
    111  1.1  pgoyette 				len = strlen((char *)buf);
    112  1.1  pgoyette 				CHOMP(buf, len, last);
    113  1.1  pgoyette 				SHA1Update(&ctx, buf, len);
    114  1.1  pgoyette 			}
    115  1.1  pgoyette 			SHA1Final(out, &ctx);
    116  1.1  pgoyette 			outlen = 20;
    117  1.1  pgoyette 		}
    118  1.1  pgoyette 		hexdump(out, outlen);
    119  1.1  pgoyette 	}
    120  1.1  pgoyette }
    121  1.1  pgoyette 
    122  1.1  pgoyette int
    123  1.1  pgoyette main(int argc, char **argv)
    124  1.1  pgoyette {
    125  1.1  pgoyette 	int ch;
    126  1.1  pgoyette 
    127  1.1  pgoyette 	while ((ch = getopt(argc, argv, "mrst")) != -1)
    128  1.1  pgoyette 		switch (ch) {
    129  1.1  pgoyette 		case 'm':
    130  1.1  pgoyette 			mflag = 1;
    131  1.1  pgoyette 			break;
    132  1.1  pgoyette 		case 'r':
    133  1.1  pgoyette 			rflag = 1;
    134  1.1  pgoyette 			break;
    135  1.1  pgoyette 		case 's':
    136  1.1  pgoyette 			sflag = 1;
    137  1.1  pgoyette 			break;
    138  1.1  pgoyette 		case 't':
    139  1.1  pgoyette 			tflag = 1;
    140  1.1  pgoyette 			break;
    141  1.1  pgoyette 		case '?':
    142  1.1  pgoyette 		default:
    143  1.1  pgoyette 			usage();
    144  1.1  pgoyette 		}
    145  1.1  pgoyette 	argc -= optind;
    146  1.1  pgoyette 	argv += optind;
    147  1.1  pgoyette 	if (argc > 0)
    148  1.1  pgoyette 		usage();
    149  1.1  pgoyette 
    150  1.1  pgoyette 	if (!(mflag || sflag))
    151  1.1  pgoyette 		mflag = 1;
    152  1.1  pgoyette 
    153  1.1  pgoyette 	if ((mflag ^ sflag) != 1)
    154  1.1  pgoyette 		usage();
    155  1.1  pgoyette 
    156  1.1  pgoyette 	if ((tflag ^ rflag) != 1)
    157  1.1  pgoyette 		usage();
    158  1.1  pgoyette 
    159  1.1  pgoyette 	if (tflag)
    160  1.1  pgoyette 		timetest();
    161  1.1  pgoyette 
    162  1.1  pgoyette 	if (rflag)
    163  1.1  pgoyette 		regress();
    164  1.1  pgoyette 
    165  1.1  pgoyette 	exit(0);
    166  1.1  pgoyette 
    167  1.1  pgoyette }
    168