Date: Thu, 19 Jul 2001 08:40:28 -0700 (PDT) From: Mark Peek <mark@whistle.com> To: freebsd-bugs@FreeBSD.org Subject: Re: bin/28988: We need more simple message digesting tools Message-ID: <200107191540.f6JFeSS36041@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/28988; it has been noted by GNATS. From: Mark Peek <mark@whistle.com> To: freebsd-gnats-submit@freebsd.org Cc: Subject: Re: bin/28988: We need more simple message digesting tools Date: Thu, 19 Jul 2001 08:32:25 -0700 At 11:00 AM -0700 7/16/01, Anders Nordby wrote: >The following reply was made to PR bin/28988; it has been noted by GNATS. > >From: Anders Nordby <anders@fix.no> >To: Dima Dorfman <dima@unixfreak.org> >Cc: FreeBSD-gnats-submit@freebsd.org >Subject: Re: bin/28988: We need more simple message digesting tools >Date: Mon, 16 Jul 2001 19:57:13 +0200 > > On Sun, Jul 15, 2001 at 05:26:22AM -0700, Dima Dorfman wrote: > >> Yes, I know openssl dgst -whatnot can do this. But the md5 >program is heavily > >> used, and thereby I think we should have sha1 and rmd160 too. > > Used by whom, and why aren't they using openssl(1)? > > MD5 checksums are quite heavily used in FreeBSD, and AFAIK most people > use the md5 program for generating MD5 fingerprints of files. At least, > I do, and I haven't heard about anyone using openssl(1) for it in > FreeBSD until recently. :-) > > > The latter is more portable and more up-to-date. > > More portable? md5(1) is just a frontend for libmd. If I make md5(1) use > libcrypto instead, would that be better? And speaking of up-to-date, > libmd must be the real issue here. It's the library that provides the > actual message-digest fingerprinting code, and is based on SSLeay. We > shouldn't be dragging old SSLeay code around when we have OpenSSL in the > base system. Over the weekend, I had worked up these patches to md(1) to use openssl instead of libmd. I was concerned about maintaining compatibility with the existing md in terms of features/options and size/location. As has been stated, openssl(1) doesn't have the same options and is way too big (dynamically linked!) to put into /sbin. # ls -l /sbin/md5 /usr/bin/openssl -r-xr-xr-x 1 root wheel 56460 Jul 8 11:36 /sbin/md5 -r-xr-xr-x 1 root wheel 256892 Jul 8 11:38 /usr/bin/openssl # file /sbin/md5 /usr/bin/openssl /sbin/md5: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), statically linked, stripped /usr/bin/openssl: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), dynamically linked (uses shared libs), stripped The patch below is a quick rewrite of md5.c to use libcrypto instead of libmd. The size is comparable and libcrypto provides a nice speed increase. Mark ----------------------- [/sbin/md5 is the old version and ./md5 is the new version] # ls -l ./md5 /sbin/md5 -rwxr-xr-x 1 root wheel 57204 Jul 15 13:57 ./md5 -r-xr-xr-x 1 root wheel 56460 Jul 8 11:36 /sbin/md5 # file ./md5 /sbin/md5 ./md5: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), statically linked, stripped /sbin/md5: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), statically linked, stripped # /sbin/md5 -t ; /sbin/md5 -x ; /sbin/md5 -r md5.c Makefile MD5 time trial. Digesting 100000 10000-byte blocks ... done Digest = 766a2bb5d24bddae466c572bcabca3ee Time = 16 seconds Speed = 62500000 bytes/second MD5 test suite: MD5 ("") = d41d8cd98f00b204e9800998ecf8427e MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661 MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72 MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0 MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = d174ab98d277d9f5a5611c2c9f419d9f MD5 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 57edf4a22be3c955ac49da2e2107b67a 2a741f9d5f4134df912f3a60a69d471f md5.c 1fc6656efb78f623fdacab2f871c8a82 Makefile # ./md5 -t ; ./md5 -x ; ./md5 -r md5.c Makefile MD5 time trial. Digesting 100000 10000-byte blocks ... done Digest = 766a2bb5d24bddae466c572bcabca3ee Time = 10 seconds Speed = 100000000 bytes/second MD5 test suite: MD5 ("") = d41d8cd98f00b204e9800998ecf8427e MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661 MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72 MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0 MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = d174ab98d277d9f5a5611c2c9f419d9f MD5 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 57edf4a22be3c955ac49da2e2107b67a 2a741f9d5f4134df912f3a60a69d471f md5.c 1fc6656efb78f623fdacab2f871c8a82 Makefile Index: sbin/md5/Makefile =================================================================== RCS file: /cvs/freebsd/src/sbin/md5/Makefile,v retrieving revision 1.6 diff -u -r1.6 Makefile --- sbin/md5/Makefile 2001/06/22 21:38:28 1.6 +++ sbin/md5/Makefile 2001/07/15 21:03:23 @@ -3,7 +3,7 @@ PROG= md5 -LDADD+= -lmd +LDADD+= -lcrypto DPADD+= ${LIBMD} WARNS?= 2 Index: sbin/md5/md5.c =================================================================== RCS file: /cvs/freebsd/src/sbin/md5/md5.c,v retrieving revision 1.25 diff -u -r1.25 md5.c --- sbin/md5/md5.c 2001/06/29 06:21:57 1.25 +++ sbin/md5/md5.c 2001/07/15 21:03:23 @@ -24,13 +24,15 @@ #include <sys/types.h> #include <err.h> -#include <md5.h> +#include <openssl/md5.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> +#define DIGESTSIZE 16 +#define HEXSIZE (DIGESTSIZE * 2 + 1) /* * Length of test block, number of test blocks. */ @@ -44,6 +46,8 @@ static void MDTimeTrial(void); static void MDTestSuite(void); static void MDFilter(int); +static char *MDFile(char *, char *); +static char *Convert2Hex(unsigned char *, char *); static void usage(void); /* Main driver. @@ -60,7 +64,7 @@ { int ch; char *p; - char buf[33]; + char buf[HEXSIZE]; while ((ch = getopt(argc, argv, "pqrs:tx")) != -1) switch (ch) { @@ -90,7 +94,7 @@ if (*argv) { do { - p = MD5File(*argv, buf); + p = MDFile(*argv, buf); if (!p) warn("%s", *argv); else @@ -113,15 +117,19 @@ MDString(const char *string) { size_t len = strlen(string); - char buf[33]; + char digest[DIGESTSIZE]; + char buf[HEXSIZE]; + Convert2Hex(MD5(string, len, digest), buf); + if (qflag) - printf("%s\n", MD5Data(string, len, buf)); + printf("%s\n", buf); else if (rflag) - printf("%s \"%s\"\n", MD5Data(string, len, buf), string); + printf("%s \"%s\"\n", buf, string); else - printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf)); + printf("MD5 (\"%s\") = %s\n", string, buf); } + /* * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. */ @@ -132,7 +140,8 @@ time_t endTime, startTime; unsigned char block[TEST_BLOCK_LEN]; unsigned int i; - char *p, buf[33]; + unsigned char digest[DIGESTSIZE]; + char buf[HEXSIZE]; printf ("MD5 time trial. Digesting %d %d-byte blocks ...", @@ -147,16 +156,17 @@ time(&startTime); /* Digest blocks */ - MD5Init(&context); + MD5_Init(&context); for (i = 0; i < TEST_BLOCK_COUNT; i++) - MD5Update(&context, block, TEST_BLOCK_LEN); - p = MD5End(&context,buf); + MD5_Update(&context, block, TEST_BLOCK_LEN); + MD5_Final(digest, &context); + Convert2Hex(digest, buf); /* Stop timer */ time(&endTime); printf(" done\n"); - printf("Digest = %s", p); + printf("Digest = %s", buf); printf("\nTime = %ld seconds\n", (long) (endTime - startTime)); /* Be careful that endTime-startTime is not zero. (Bug fix from Ric * Anderson, ric@Artisoft.COM.) */ @@ -194,15 +204,63 @@ MD5_CTX context; unsigned int len; unsigned char buffer[BUFSIZ]; - char buf[33]; + unsigned char digest[DIGESTSIZE]; + char buf[HEXSIZE]; - MD5Init(&context); + MD5_Init(&context); while ((len = fread(buffer, 1, BUFSIZ, stdin))) { if (tee && len != fwrite(buffer, 1, len, stdout)) err(1, "stdout"); - MD5Update(&context, buffer, len); + MD5_Update(&context, buffer, len); + } + MD5_Final(digest, &context); + Convert2Hex(digest, buf); + + printf("%s\n", buf); +} + +/* + * Digests the file and returns the hex string result. + */ +static char * +MDFile(char *filename, char *buf) +{ + MD5_CTX context; + unsigned int len; + unsigned char buffer[BUFSIZ]; + unsigned char digest[DIGESTSIZE]; + FILE *fp; + + if ((fp = fopen(filename, "r")) == NULL) { + return NULL; + } + MD5_Init(&context); + while ((len = fread(buffer, 1, BUFSIZ, fp))) { + MD5_Update(&context, buffer, len); + } + + MD5_Final(digest, &context); + Convert2Hex(digest, buf); + + return buf; +} + +/* + * Convert a digest string into an ascii hex string. + */ +static char * +Convert2Hex(unsigned char *digest, char *buf) +{ + static const char hex[]="0123456789abcdef"; + int i; + + for (i = 0; i < DIGESTSIZE; i++) { + buf[i+i] = hex[digest[i] >> 4]; + buf[i+i+1] = hex[digest[i] & 0x0f]; } - printf("%s\n", MD5End(&context,buf)); + buf[i+i] = '\0'; + + return buf; } static void To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200107191540.f6JFeSS36041>