1272343Sngie/*	$NetBSD: h_hash.c,v 1.1 2011/01/02 22:03:25 pgoyette Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2000 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie/*
30272343Sngie * Combined MD5/SHA1 time and regression test.
31272343Sngie */
32272343Sngie
33272343Sngie#include <stdio.h>
34272343Sngie#include <stdlib.h>
35272343Sngie#include <unistd.h>
36272343Sngie#include <string.h>
37272343Sngie#include <md5.h>
38272343Sngie#include <sha1.h>
39272343Sngie
40272343Sngie
41272343Sngieint mflag, rflag, sflag, tflag;
42272343Sngie
43272343Sngiestatic void
44272343Sngieusage(void)
45272343Sngie{
46272343Sngie	(void)fprintf(stderr,
47272343Sngie	    "Usage:\t%s -r[ms] < test-file\n"
48272343Sngie	    "\t%s -t[ms]\n",
49272343Sngie	    getprogname(), getprogname());
50272343Sngie	exit(1);
51272343Sngie	/* NOTREACHED */
52272343Sngie}
53272343Sngie
54272343Sngiestatic void
55272343Sngiehexdump (unsigned char *buf, int len)
56272343Sngie{
57272343Sngie	int i;
58272343Sngie	for (i=0; i<len; i++) {
59272343Sngie		printf("%02x", buf[i]);
60272343Sngie	}
61272343Sngie	printf("\n");
62272343Sngie}
63272343Sngie
64272343Sngie
65272343Sngiestatic void
66272343Sngietimetest(void)
67272343Sngie{
68272343Sngie	printf("sorry, not yet\n");
69272343Sngie}
70272343Sngie
71272343Sngie#define CHOMP(buf, len, last) 				\
72272343Sngie	if ((len > 0) &&				\
73272343Sngie	    (buf[len-1] == '\n')) {			\
74272343Sngie		buf[len-1] = '\0';			\
75272343Sngie		len--;					\
76272343Sngie		last = 1;				\
77272343Sngie	}
78272343Sngie
79272343Sngiestatic void
80272343Sngieregress(void)
81272343Sngie{
82272343Sngie	unsigned char buf[1024];
83272343Sngie	unsigned char out[20];
84272343Sngie	int len, outlen, last;
85272343Sngie
86272343Sngie	while (fgets((char *)buf, sizeof(buf), stdin) != NULL) {
87272343Sngie		last = 0;
88272343Sngie
89272343Sngie		len = strlen((char *)buf);
90272343Sngie		CHOMP(buf, len, last);
91272343Sngie		if (mflag) {
92272343Sngie			MD5_CTX ctx;
93272343Sngie
94272343Sngie			MD5Init(&ctx);
95272343Sngie			MD5Update(&ctx, buf, len);
96272343Sngie			while (!last &&
97272343Sngie			    fgets((char *)buf, sizeof(buf), stdin) != NULL) {
98272343Sngie				len = strlen((char *)buf);
99272343Sngie				CHOMP(buf, len, last);
100272343Sngie				MD5Update(&ctx, buf, len);
101272343Sngie			}
102272343Sngie			MD5Final(out, &ctx);
103272343Sngie			outlen = 16;
104272343Sngie		} else {
105272343Sngie			SHA1_CTX ctx;
106272343Sngie
107272343Sngie			SHA1Init(&ctx);
108272343Sngie			SHA1Update(&ctx, buf, len);
109272343Sngie			while (!last &&
110272343Sngie			    fgets((char *)buf, sizeof(buf), stdin) != NULL) {
111272343Sngie				len = strlen((char *)buf);
112272343Sngie				CHOMP(buf, len, last);
113272343Sngie				SHA1Update(&ctx, buf, len);
114272343Sngie			}
115272343Sngie			SHA1Final(out, &ctx);
116272343Sngie			outlen = 20;
117272343Sngie		}
118272343Sngie		hexdump(out, outlen);
119272343Sngie	}
120272343Sngie}
121272343Sngie
122272343Sngieint
123272343Sngiemain(int argc, char **argv)
124272343Sngie{
125272343Sngie	int ch;
126272343Sngie
127272343Sngie	while ((ch = getopt(argc, argv, "mrst")) != -1)
128272343Sngie		switch (ch) {
129272343Sngie		case 'm':
130272343Sngie			mflag = 1;
131272343Sngie			break;
132272343Sngie		case 'r':
133272343Sngie			rflag = 1;
134272343Sngie			break;
135272343Sngie		case 's':
136272343Sngie			sflag = 1;
137272343Sngie			break;
138272343Sngie		case 't':
139272343Sngie			tflag = 1;
140272343Sngie			break;
141272343Sngie		case '?':
142272343Sngie		default:
143272343Sngie			usage();
144272343Sngie		}
145272343Sngie	argc -= optind;
146272343Sngie	argv += optind;
147272343Sngie	if (argc > 0)
148272343Sngie		usage();
149272343Sngie
150272343Sngie	if (!(mflag || sflag))
151272343Sngie		mflag = 1;
152272343Sngie
153272343Sngie	if ((mflag ^ sflag) != 1)
154272343Sngie		usage();
155272343Sngie
156272343Sngie	if ((tflag ^ rflag) != 1)
157272343Sngie		usage();
158272343Sngie
159272343Sngie	if (tflag)
160272343Sngie		timetest();
161272343Sngie
162272343Sngie	if (rflag)
163272343Sngie		regress();
164272343Sngie
165272343Sngie	exit(0);
166272343Sngie
167272343Sngie}
168