-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
47 lines (43 loc) · 1.74 KB
/
Copy pathtest.c
File metadata and controls
47 lines (43 loc) · 1.74 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#include <zlib.h>
#include <time.h>
#define N 1000000
#define s_to_us 1000000
//chunksize = {512, 1024, 2048, 4096, 8192, 16384}; //0.5kB, 1kB, 2kB, 4kB, 8kB, 16kB (Unit: Byte)
//gcc test.c -o test -L/usr/local/opt/openssl@1.1/lib -I/usr/local/opt/openssl@1.1/include -lcrypto -lz
//./test 0.5 <linux-5.3.5.tar.xz
unsigned char digest[SHA_DIGEST_LENGTH];//SHA_DIGEST_LENGTH = 20 (Unit: Byte)
int main(int argc, char *argv[])
{
int chunksize = 1024 * atof(argv[1]);
unsigned char buffer[chunksize];
int read_bytes = fread(buffer, 1, chunksize, stdin);//input file from command line, read only csize bytes
printf("read %d bytes data\n", read_bytes);
clock_t s = clock();
//printf("start clock = %lu\n", s);
for(int i = 0; i < N; i++)
SHA1(buffer, chunksize, digest);//SHA computation of N times
clock_t e = clock();
//printf("end clock = %lu\n", e);
//printf("time(end - start) = %lu\n", (e-s));
double time = ((double)(e-s)/(double)CLOCKS_PER_SEC)*s_to_us;
printf("SHA average execution time = %.10f us\n", time/N);
//print SHA value
char mdString[SHA_DIGEST_LENGTH*2+1];
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
//printf("SHA1 digest: %s\n", mdString);
uint32_t crc = crc32(0L, Z_NULL, 0);
s = clock();
//printf("start clock = %lu\n", s);
for(int i = 0; i < N; i++)
crc32(crc, buffer, chunksize);//CRC computation of N times
e = clock();
//printf("end clock = %lu\n", e);
time = ((double)(e-s)/(double)CLOCKS_PER_SEC)*s_to_us;
printf("CRC average execution time = %.10f us\n", time/N);
return 0;
}