I wanted to find out how to interface with SHA through
OpenSSL, so I wrote this little program below and ran some tests.
Turns out
SHA1 runs faster than
SHA. I kind of figured the reverse. I ran a 94 megabyte file through the program below on a single processor 1Ghz PIII SLT2 and got...
| SHA |
SHA1 |
| 69.8 MB/s |
45.1 MB/s |
(shatest.c)
#include <openssl/sha.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024*16
#define SHA 1
/* Dumps out a SHA string
*/
void pt(unsigned char *md)
{
int i;
for (i=0; i<SHA_DIGEST_LENGTH; i++)
{ printf("%02x",md[i]);
}
printf("\n");
}
long do_fp_test(FILE *f, char* result, int SHA1mode)
{
SHA_CTX c;
int fd;
int i;
long bytesRead = 0;
unsigned char buf[BUFSIZE];
fd=fileno(f);
if (SHA1mode)
{ SHA1_Init(&c);
}
else
{ SHA_Init(&c);
}
for (;;)
{
i=read(fd,buf,BUFSIZE);
bytesRead += i;
if (i <= 0) break;
if (SHA1mode)
{ SHA1_Update(&c,buf,(unsigned long)i);
}
else
{ SHA_Update(&c,buf,(unsigned long)i);
}
}
if (SHA1mode)
{ SHA1_Final(&(result[0]),&c);
}
else
{ SHA_Final(&(result[0]),&c);
}
return bytesRead;
}
int main(int argc, char **argv)
{
FILE* myFile;
unsigned char md[SHA_DIGEST_LENGTH];
struct timeval start, end;
double total_time;
long bytesProcessed = 0;
float processingRate = 0;
int doSHA1 = 1;
if (argc < 2)
{
printf("usage: shatest <filename> [SHA] (SHA1 by default)\n");
}
else
{
if (argc > 2)
{ if (!strcmp(argv[2], "SHA"))
{ doSHA1 = 0;
}
}
printf("Computing %s for %s...",
(doSHA1 ? "SHA1" : "SHA"), argv[1]);
myFile = fopen(argv[1], "r");
if (myFile)
{
/* start the timer
*/
gettimeofday(&start, NULL);
bytesProcessed = do_fp_test(myFile, md, doSHA1);
gettimeofday(&end, NULL);
printf("OK!\n");
pt(md);
total_time =
(end.tv_sec + (end.tv_usec * 0.000001)) -
(start.tv_sec + (start.tv_usec * 0.000001));
processingRate = ((float)bytesProcessed / total_time)/1000000;
printf("Total time for test: %f\n", total_time);
printf("Bytes processed: %d\n", bytesProcessed);
printf("Processing Speed: %f MB/s\n", processingRate);
}
else
{ printf("FAILED! Can't open file.\n");
}
}
}
(Makefile)
OPENSSLDIR = /usr/local/ssl # openSSL directory
INCLUDE = -I$(OPENSSLDIR)/crypto/sha
LIB = $(OPENSSLDIR)/libcrypto.a $(OPENSSLDIR)/libssl.a
shatest: shatest.c
gcc -o shatest shatest.c ${INCLUDE} ${LIB} -static
clean:
rm *.o core
--
MattWalsh - 09 Feb 2002