sha1speed.c
2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* \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: