sha1speed.c 2.37 KB
/**
 * \file
 * Small test for out sha1 implementation
 *
 * Copyright © 2013, Copperfasten Technologies, Teoranta.  All rights
 * reserved.  Unpublished rights reserved under the copyright laws of
 * the United States and/or the Republic of Ireland.
 *
 * The software contained herein is proprietary to and embodies the
 * confidential technology of Copperfasten Technologies, Teoranta.
 * Possession, use, duplication or dissemination of the software and
 * media is authorized only pursuant to a valid written license from
 * Copperfasten Technologies, Teoranta.
 *
 * \author  Georg Hopp <ghopp@spamtitan.com>
 * \version SVN: $id: $
 * \copyright Copyright © 2013, Copperfasten Technologies
 */
#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];
	unsigned char digest[20];
	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, &ctx);
	}
	stop = clock();
	printf(
			"done\nResult: %08x%08x%08x%08x%08x\n",
			htonl(((uint32_t *)&digest)[0]),
			htonl(((uint32_t *)&digest)[1]),
			htonl(((uint32_t *)&digest)[2]),
			htonl(((uint32_t *)&digest)[3]),
			htonl(((uint32_t *)&digest)[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, &ctx);
	}
	stop = clock();
	printf(
			"Done\nResult: %08x%08x%08x%08x%08x\n",
			htonl(((uint32_t *)&digest)[0]),
			htonl(((uint32_t *)&digest)[1]),
			htonl(((uint32_t *)&digest)[2]),
			htonl(((uint32_t *)&digest)[3]),
			htonl(((uint32_t *)&digest)[4]));
	printf("CPU time Ours: %f\n", (double)(stop - start) / CLOCKS_PER_SEC);

	return 0;
}

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