Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 6 Jan 2012 02:27:43 +0000 (UTC)
From:      Eitan Adler <eadler@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r229681 - stable/8/sbin/md5
Message-ID:  <201201060227.q062RhDi067544@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: eadler (ports committer)
Date: Fri Jan  6 02:27:43 2012
New Revision: 229681
URL: http://svn.freebsd.org/changeset/base/229681

Log:
  MFC r227488:
  
  - add "check" option to MD5 and friends to compare files against known hash.
  
  Approved by: cperciva

Modified:
  stable/8/sbin/md5/md5.1
  stable/8/sbin/md5/md5.c
Directory Properties:
  stable/8/sbin/md5/   (props changed)

Modified: stable/8/sbin/md5/md5.1
==============================================================================
--- stable/8/sbin/md5/md5.1	Fri Jan  6 02:24:29 2012	(r229680)
+++ stable/8/sbin/md5/md5.1	Fri Jan  6 02:27:43 2012	(r229681)
@@ -8,18 +8,22 @@
 .Sh SYNOPSIS
 .Nm md5
 .Op Fl pqrtx
+.Op Fl c Ar string
 .Op Fl s Ar string
 .Op Ar
 .Nm sha1
 .Op Fl pqrtx
+.Op Fl c Ar string
 .Op Fl s Ar string
 .Op Ar
 .Nm sha256
 .Op Fl pqrtx
+.Op Fl c Ar string
 .Op Fl s Ar string
 .Op Ar
 .Nm rmd160
 .Op Fl pqrtx
+.Op Fl c Ar string
 .Op Fl s Ar string
 .Op Ar
 .Sh DESCRIPTION
@@ -73,6 +77,9 @@ precede any files named on the command l
 The hexadecimal checksum of each file listed on the command line is printed
 after the options are processed.
 .Bl -tag -width indent
+.It Fl c Ar string
+Compare files to this md5 string. (Note that this option is not yet useful
+if multiple files are specified.)
 .It Fl s Ar string
 Print a checksum of the given
 .Ar string .
@@ -101,7 +108,8 @@ The
 and
 .Nm rmd160
 utilities exit 0 on success,
-and 1 if at least one of the input files could not be read.
+1 if at least one of the input files could not be read,
+and 2 if at least one file does not have the same hash as the -c option.
 .Sh SEE ALSO
 .Xr cksum 1 ,
 .Xr md5 3 ,

Modified: stable/8/sbin/md5/md5.c
==============================================================================
--- stable/8/sbin/md5/md5.c	Fri Jan  6 02:24:29 2012	(r229680)
+++ stable/8/sbin/md5/md5.c	Fri Jan  6 02:27:43 2012	(r229681)
@@ -44,6 +44,8 @@ __FBSDID("$FreeBSD$");
 int qflag;
 int rflag;
 int sflag;
+unsigned char* checkAgainst;
+int	checksFailed;
 
 typedef void (DIGEST_Init)(void *);
 typedef void (DIGEST_Update)(void *, const unsigned char *, size_t);
@@ -138,8 +140,13 @@ main(int argc, char *argv[])
  		digest = 0;
 
 	failed = 0;
-	while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
+	checkAgainst = NULL;
+	checksFailed = 0;
+	while ((ch = getopt(argc, argv, "c:pqrs:tx")) != -1)
 		switch (ch) {
+		case 'c':
+			checkAgainst = optarg;
+			break;
 		case 'p':
 			MDFilter(&Algorithm[digest], 1);
 			break;
@@ -173,12 +180,19 @@ main(int argc, char *argv[])
 				failed++;
 			} else {
 				if (qflag)
-					printf("%s\n", p);
+					printf("%s", p);
 				else if (rflag)
-					printf("%s %s\n", p, *argv);
+					printf("%s %s", p, *argv);
 				else
-					printf("%s (%s) = %s\n",
+					printf("%s (%s) = %s",
 					    Algorithm[digest].name, *argv, p);
+				if (checkAgainst && strcmp(checkAgainst,p))
+				{
+					checksFailed++;
+					if (!qflag)
+						printf(" [ Failed ]");
+				}
+				printf("\n");
 			}
 		} while (*++argv);
 	} else if (!sflag && (optind == 1 || qflag || rflag))
@@ -186,6 +200,8 @@ main(int argc, char *argv[])
 
 	if (failed != 0)
 		return (1);
+	if (checksFailed != 0)
+		return (2);
 
 	return (0);
 }
@@ -198,12 +214,20 @@ MDString(Algorithm_t *alg, const char *s
 	size_t len = strlen(string);
 	char buf[HEX_DIGEST_LENGTH];
 
+	alg->Data(string,len,buf);
 	if (qflag)
-		printf("%s\n", alg->Data(string, len, buf));
+		printf("%s", buf);
 	else if (rflag)
-		printf("%s \"%s\"\n", alg->Data(string, len, buf), string);
+		printf("%s \"%s\"", buf, string);
 	else
-		printf("%s (\"%s\") = %s\n", alg->name, string, alg->Data(string, len, buf));
+		printf("%s (\"%s\") = %s", alg->name, string, buf);
+	if (checkAgainst && strcmp(buf,checkAgainst))
+	{
+		checksFailed++;
+		if (!qflag)
+			printf(" [ failed ]");
+	}
+	printf("\n");
 }
 /*
  * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201201060227.q062RhDi067544>