Hash utilities: maintain a hash state object, not a bundle of arguments.
[u/mdw/catacomb] / dsig.c
diff --git a/dsig.c b/dsig.c
index 5006423..4847ac8 100644 (file)
--- a/dsig.c
+++ b/dsig.c
@@ -467,6 +467,7 @@ static int sign(int argc, char *argv[])
   key_file kf;
   key *k;
   sig *s;
+  fhashstate fh;
   time_t exp = KEXP_EXPIRE;
   unsigned verb = 0;
   const char *ifile = 0, *hfile = 0;
@@ -662,12 +663,13 @@ static int sign(int argc, char *argv[])
 
       /* --- Read the next filename to hash --- */
 
+      fhash_init(&fh, GH_CLASS(s->h), f | FHF_BINARY);
       breset(&b);
       if (getstring(ifp, &b.d, GSF_FILE | f))
        break;
       b.tag = T_FILE;
       DENSURE(&b.b, GH_CLASS(s->h)->hashsz);
-      if (fhash(GH_CLASS(s->h), f | FHF_BINARY, b.d.buf, b.b.buf)) {
+      if (fhash(&fh, b.d.buf, b.b.buf)) {
        moan("error reading `%s': %s", b.d.buf, strerror(errno));
        f |= f_bogus;
       } else {
@@ -678,6 +680,7 @@ static int sign(int argc, char *argv[])
        }
        bemit(&b, ofp, s->h, f & f_bin);
       }
+      fhash_free(&fh);
     }
   }
 
@@ -744,6 +747,7 @@ static int verify(int argc, char *argv[])
   sig *s;
   dstr d = DSTR_INIT;
   const char *err;
+  fhashstate fh;
   FILE *fp;
   block b;
   int e;
@@ -853,6 +857,7 @@ static int verify(int argc, char *argv[])
   if (!(f & f_nocheck) && verb && (err = s->ops->check(s)) != 0)
     printf("WARN public key fails check: %s", err);
 
+  fhash_init(&fh, GH_CLASS(s->h), f | FHF_BINARY);
   for (;;) {
     switch (e) {
       case T_COMMENT:
@@ -885,7 +890,7 @@ static int verify(int argc, char *argv[])
       case T_FILE:
        DRESET(&d);
        DENSURE(&d, GH_CLASS(s->h)->hashsz);
-       if (fhash(GH_CLASS(s->h), f | FHF_BINARY, b.d.buf, d.buf)) {
+       if (fhash(&fh, b.d.buf, d.buf)) {
          if (verb > 1) {
            printf("BAD error reading file `%s': %s\n",
                    b.d.buf, strerror(errno));
@@ -926,6 +931,7 @@ static int verify(int argc, char *argv[])
     }
   }
 done:
+  fhash_free(&fh);
   bdestroy(&b);
   dstr_destroy(&d);
   freesig(s);