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> 38276478Sngie#ifdef __NetBSD__ 39272343Sngie#include <sha1.h> 40276478Sngie#endif 41272343Sngie 42276478Sngie#ifdef __FreeBSD__ 43276478Sngie#include <sha.h> 44276478Sngie#endif 45272343Sngie 46272343Sngieint mflag, rflag, sflag, tflag; 47272343Sngie 48272343Sngiestatic void 49272343Sngieusage(void) 50272343Sngie{ 51272343Sngie (void)fprintf(stderr, 52272343Sngie "Usage:\t%s -r[ms] < test-file\n" 53272343Sngie "\t%s -t[ms]\n", 54272343Sngie getprogname(), getprogname()); 55272343Sngie exit(1); 56272343Sngie /* NOTREACHED */ 57272343Sngie} 58272343Sngie 59272343Sngiestatic void 60272343Sngiehexdump (unsigned char *buf, int len) 61272343Sngie{ 62272343Sngie int i; 63272343Sngie for (i=0; i<len; i++) { 64272343Sngie printf("%02x", buf[i]); 65272343Sngie } 66272343Sngie printf("\n"); 67272343Sngie} 68272343Sngie 69272343Sngie 70272343Sngiestatic void 71272343Sngietimetest(void) 72272343Sngie{ 73272343Sngie printf("sorry, not yet\n"); 74272343Sngie} 75272343Sngie 76272343Sngie#define CHOMP(buf, len, last) \ 77272343Sngie if ((len > 0) && \ 78272343Sngie (buf[len-1] == '\n')) { \ 79272343Sngie buf[len-1] = '\0'; \ 80272343Sngie len--; \ 81272343Sngie last = 1; \ 82272343Sngie } 83272343Sngie 84272343Sngiestatic void 85272343Sngieregress(void) 86272343Sngie{ 87272343Sngie unsigned char buf[1024]; 88272343Sngie unsigned char out[20]; 89272343Sngie int len, outlen, last; 90272343Sngie 91272343Sngie while (fgets((char *)buf, sizeof(buf), stdin) != NULL) { 92272343Sngie last = 0; 93272343Sngie 94272343Sngie len = strlen((char *)buf); 95272343Sngie CHOMP(buf, len, last); 96272343Sngie if (mflag) { 97272343Sngie MD5_CTX ctx; 98272343Sngie 99272343Sngie MD5Init(&ctx); 100272343Sngie MD5Update(&ctx, buf, len); 101272343Sngie while (!last && 102272343Sngie fgets((char *)buf, sizeof(buf), stdin) != NULL) { 103272343Sngie len = strlen((char *)buf); 104272343Sngie CHOMP(buf, len, last); 105272343Sngie MD5Update(&ctx, buf, len); 106272343Sngie } 107272343Sngie MD5Final(out, &ctx); 108272343Sngie outlen = 16; 109272343Sngie } else { 110276478Sngie#ifdef __FreeBSD__ 111276478Sngie SHA_CTX ctx; 112276478Sngie 113276478Sngie SHA1_Init(&ctx); 114276478Sngie SHA1_Update(&ctx, buf, len); 115276478Sngie#else 116272343Sngie SHA1_CTX ctx; 117272343Sngie 118272343Sngie SHA1Init(&ctx); 119272343Sngie SHA1Update(&ctx, buf, len); 120276478Sngie#endif 121272343Sngie while (!last && 122272343Sngie fgets((char *)buf, sizeof(buf), stdin) != NULL) { 123272343Sngie len = strlen((char *)buf); 124272343Sngie CHOMP(buf, len, last); 125276478Sngie#ifdef __FreeBSD__ 126276478Sngie SHA1_Update(&ctx, buf, len); 127276478Sngie#else 128272343Sngie SHA1Update(&ctx, buf, len); 129276478Sngie#endif 130272343Sngie } 131276478Sngie#ifdef __FreeBSD__ 132276478Sngie SHA1_Final(out, &ctx); 133276478Sngie#else 134272343Sngie SHA1Final(out, &ctx); 135276478Sngie#endif 136272343Sngie outlen = 20; 137272343Sngie } 138272343Sngie hexdump(out, outlen); 139272343Sngie } 140272343Sngie} 141272343Sngie 142272343Sngieint 143272343Sngiemain(int argc, char **argv) 144272343Sngie{ 145272343Sngie int ch; 146272343Sngie 147272343Sngie while ((ch = getopt(argc, argv, "mrst")) != -1) 148272343Sngie switch (ch) { 149272343Sngie case 'm': 150272343Sngie mflag = 1; 151272343Sngie break; 152272343Sngie case 'r': 153272343Sngie rflag = 1; 154272343Sngie break; 155272343Sngie case 's': 156272343Sngie sflag = 1; 157272343Sngie break; 158272343Sngie case 't': 159272343Sngie tflag = 1; 160272343Sngie break; 161272343Sngie case '?': 162272343Sngie default: 163272343Sngie usage(); 164272343Sngie } 165272343Sngie argc -= optind; 166272343Sngie argv += optind; 167272343Sngie if (argc > 0) 168272343Sngie usage(); 169272343Sngie 170272343Sngie if (!(mflag || sflag)) 171272343Sngie mflag = 1; 172272343Sngie 173272343Sngie if ((mflag ^ sflag) != 1) 174272343Sngie usage(); 175272343Sngie 176272343Sngie if ((tflag ^ rflag) != 1) 177272343Sngie usage(); 178272343Sngie 179272343Sngie if (tflag) 180272343Sngie timetest(); 181272343Sngie 182272343Sngie if (rflag) 183272343Sngie regress(); 184272343Sngie 185272343Sngie exit(0); 186272343Sngie 187272343Sngie} 188