Perf testing program for baseline analysis.
[sha1collisiondetection.git] / perf / random_hashing.cpp
blobfc8520484f0cc035022d7de624e92b189b0cee2c
1 #include <string.h>
2 #include <stdio.h>
3 #include <algorithm>
5 #include <iostream>
7 #include "sha1.h"
8 #include "ubc_check.h"
10 #include "test_util_lib.h"
12 using namespace std;
14 int GenRandomBytes(
15 unsigned char* pb,
16 size_t cb,
17 br::mt19937 &rng)
19 size_t i;
21 for (i = 0; i < cb; i++)
23 pb[i] = (unsigned char)rng();
26 return i;
30 int HashRandomBytes(
31 size_t cntBytesToHash,
32 unsigned int uiSeed)
34 unsigned char hash[20];
36 unsigned char buffer_to_hash[64];
38 SHA1_CTX ctx;
40 int i = cntBytesToHash;
42 br::mt19937 rng(uiSeed);
43 cout << "Hashing...";
44 SHA1DCInit(&ctx);
46 while (0 < i)
48 unsigned int cb = min(64, i);
50 cb = GenRandomBytes(buffer_to_hash, cb, rng);
52 SHA1DCUpdate(&ctx, (char *)buffer_to_hash, cb);
54 i -= cb;
57 SHA1DCFinal(hash, &ctx);
59 cout << "Done." << endl;
61 if (ctx.found_collision)
63 cout << "Collision found." << endl;
66 return 0;
70 int UBCVerifyRandomBytes(
71 size_t cntBytesToHash,
72 unsigned int uiSeed)
74 unsigned char buffer_to_hash[64];
76 uint32_t expanded_message[80];
78 uint32_t dvmask[1] = { 0 };
79 uint32_t dvmask_verify[1] = { 0 };
81 int i = cntBytesToHash;
83 br::mt19937 rng(uiSeed);
84 cout << "Unavoidable bit checking...";
86 while (0 < i)
88 unsigned int cb = min(64, i);
90 cb = GenRandomBytes(buffer_to_hash, cb, rng);
92 memset(dvmask, 0, sizeof(dvmask));
93 memset(dvmask_verify, 0, sizeof(dvmask_verify));
95 sha1_compression_W((uint32_t*)buffer_to_hash, expanded_message);
97 ubc_check(expanded_message, dvmask);
98 //ubc_check_verify(expanded_message, dvmask_verify);
100 if (0 != memcmp(dvmask, dvmask_verify, sizeof(dvmask)))
102 printf("Calculated dvmask does not equal dvmask verify output.\n");
105 i -= cb;
108 cout << "Done." << endl;
110 return 0;