sha1speed.c 2.37 KB
/**
 * \file
 * Small test for out sha1 implementation
 *
 * Copyright © 2014 Georg Hopp
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <sys/types.h>
#include <stdint.h>
#include <time.h>

#ifdef __MINGW32__
#include <winsock.h>
#else
#include <arpa/inet.h>
#endif

#include <openssl/sha.h>
#include "tr/sha1.h"

#define TIMES	100000

int
main(int argc, char * argv [])
{
	unsigned char data[16384];
	union {
		unsigned char bytes[20];
		uint32_t      ints[5];
	} digest;
	size_t        got;
	int           i;
	clock_t       start, stop;

	/*
	 * read at maximum 16384 byte from stdin to build the sha1
	 * of it 100000 times....first as a reference with sha1 and
	 * then with out implementation.
	 */
	got = fread(data, 1, 16384, stdin);

	printf("Generate %d hashes with openssl ... ", TIMES);
	start = clock();
	for (i=0; i<TIMES; i++) {
		SHA_CTX ctx;

		SHA1_Init(&ctx);
		SHA1_Update(&ctx, data, got);
		SHA1_Final(digest.bytes, &ctx);
	}
	stop = clock();
	printf(
			"done\nResult: %08x%08x%08x%08x%08x\n",
			htonl(digest.ints[0]),
			htonl(digest.ints[1]),
			htonl(digest.ints[2]),
			htonl(digest.ints[3]),
			htonl(digest.ints[4]));
	printf("CPU time OpenSSL: %f\n",
			(double)(stop - start) / CLOCKS_PER_SEC);

	puts("--------------------");

	printf("Generate %d hashes with our sha1 ... ", TIMES);
	start = clock();
	for (i=0; i<TIMES; i++) {
		TR_SHA_CTX ctx;

		TR_SHA1_Init(&ctx);
		TR_SHA1_Update(&ctx, data, got);
		TR_SHA1_Final(digest.bytes, &ctx);
	}
	stop = clock();
	printf(
			"Done\nResult: %08x%08x%08x%08x%08x\n",
			htonl(digest.ints[0]),
			htonl(digest.ints[1]),
			htonl(digest.ints[2]),
			htonl(digest.ints[3]),
			htonl(digest.ints[4]));
	printf("CPU time Ours: %f\n", (double)(stop - start) / CLOCKS_PER_SEC);

	return 0;
}

// vim: set ts=4 sw=4: