Browse Source

Make digest handling more optimizer save by using a union

1.0.0
Georg Hopp 11 years ago
parent
commit
73e6d512fe
  1. 9
      Makefile.am
  2. 29
      src/sha1speed.c

9
Makefile.am

@ -16,5 +16,12 @@ coverage-html:
-$(MAKE) -C tests $(AM_MAKEFLAGS) -k $@
endif
REGEX_CCLASS=/^[ \t]*\(TR_CLASS\|TR_INTERFACE\)\(([a-zA-Z0-9_]+)/\2/d,definition/
tags:
ctags -R -V --langdef=cclass --langmap=cclass:.h --regex-cclass='/^[ \t]*\(TR_CLASS\|TR_INTERFACE\)\(([a-zA-Z0-9_]+)/\2/d,definition/'
@ctags -R -V --langdef=cclass --langmap=cclass:.h \
--regex-cclass='$(REGEX_CCLASS)'
loc:
@find src/ include/ -not -path testers -and -name "*.[ch]" \
-exec sed '/\/\*/,/\*\//d;/\/\//d' {} \; | wc -l

29
src/sha1speed.c

@ -37,7 +37,10 @@ int
main(int argc, char * argv [])
{
unsigned char data[16384];
unsigned char digest[20];
union {
unsigned char bytes[20];
uint32_t ints[5];
} digest;
size_t got;
int i;
clock_t start, stop;
@ -56,16 +59,16 @@ main(int argc, char * argv [])
SHA1_Init(&ctx);
SHA1_Update(&ctx, data, got);
SHA1_Final(digest, &ctx);
SHA1_Final(digest.bytes, &ctx);
}
stop = clock();
printf(
"done\nResult: %08x%08x%08x%08x%08x\n",
htonl(((uint32_t *)&digest)[0]),
htonl(((uint32_t *)&digest)[1]),
htonl(((uint32_t *)&digest)[2]),
htonl(((uint32_t *)&digest)[3]),
htonl(((uint32_t *)&digest)[4]));
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);
@ -78,16 +81,16 @@ main(int argc, char * argv [])
TR_SHA1_Init(&ctx);
TR_SHA1_Update(&ctx, data, got);
TR_SHA1_Final(digest, &ctx);
TR_SHA1_Final(digest.bytes, &ctx);
}
stop = clock();
printf(
"Done\nResult: %08x%08x%08x%08x%08x\n",
htonl(((uint32_t *)&digest)[0]),
htonl(((uint32_t *)&digest)[1]),
htonl(((uint32_t *)&digest)[2]),
htonl(((uint32_t *)&digest)[3]),
htonl(((uint32_t *)&digest)[4]));
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;

Loading…
Cancel
Save