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
96
97
98
99
/**
* \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: