Added SILC Thread Queue API
[silc.git] / lib / silccrypt / tests / test_hash.c
1 /*
2
3   test_hash.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21
22 #define HASH_LEN 0x0002ffff     /* hash data len (at least) */
23 #define HASH_ROUND 256          /* hash rounds (at least) */
24 #define HASH_MIN_TIME 2.0       /* seconds to run the test (at least) */
25
26 SilcTimerStruct timer;
27 SilcHash hash;
28
29 int main(int argc, char **argv)
30 {
31   SilcUInt64 sec;
32   SilcUInt32 usec;
33   double totsec;
34   unsigned char *data;
35   SilcUInt32 rounds;
36   unsigned char digest[SILC_HASH_MAXLEN];
37   int i, k;
38
39   data = malloc(HASH_LEN * sizeof(*data));
40   if (!data)
41     exit(1);
42
43   for (i = 0; i < HASH_LEN; i++)
44     data[i] = i % 255;
45
46   silc_timer_synchronize(&timer);
47
48   for (i = 0; silc_default_hash[i].name; i++) {
49     if (!silc_hash_alloc(silc_default_hash[i].name, &hash))
50       exit(1);
51     silc_hash_init(hash);
52
53     rounds = HASH_ROUND;
54
55   retry:
56     silc_timer_start(&timer);
57     for (k = 0; k < rounds; k++)
58       silc_hash_update(hash, data, HASH_LEN);
59     silc_timer_stop(&timer);
60     silc_hash_final(hash, digest);
61
62     silc_timer_value(&timer, &sec, &usec);
63     totsec = (double)sec;
64     totsec += ((double)usec / (double)(1000 * 1000));
65     if (totsec < HASH_MIN_TIME) {
66       rounds *= 2;
67       goto retry;
68     }
69
70     printf("%s:\t%.2f KB (%.2f MB) / sec (total test time %.2f secs)\n",
71            silc_default_hash[i].name,
72            (((double)(HASH_LEN * rounds) / 1024.0) / totsec),
73            (((double)(HASH_LEN * rounds) / (1024.0 * 1024.0)) / totsec),
74            totsec);
75
76     silc_hash_free(hash);
77   }
78
79   return 0;
80 }