[PATCH 1/2] Add an utility to calculate hashes over a set of files

Dmitry Eremin-Solenikov dbaryshkov at gmail.com
Sat Sep 21 23:18:27 CEST 2013


* src/gchash.c: New.

--
An utility like rhash that has the ability to calculate different hashes
over a set of files it usefull. Add gchash utility to calculate hashes
supported by libgcrypt.

Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov at gmail.com>
---
 src/Makefile.am |   6 ++-
 src/gchash.c    | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 src/gchash.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 3bc27c8..ebb42f7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -29,7 +29,7 @@ m4data_DATA = libgcrypt.m4
 include_HEADERS = gcrypt.h
 
 lib_LTLIBRARIES = libgcrypt.la
-bin_PROGRAMS = dumpsexp hmac256 mpicalc
+bin_PROGRAMS = dumpsexp hmac256 mpicalc gchash
 if USE_RANDOM_DAEMON
 sbin_PROGRAMS = gcryptrnd
 bin_PROGRAMS += getrandom
@@ -128,6 +128,10 @@ dumpsexp_SOURCES = dumpsexp.c
 dumpsexp_CFLAGS = $(arch_gpg_error_cflags)
 dumpsexp_LDADD = $(arch_gpg_error_libs)
 
+gchash_SOURCES = gchash.c
+gchash_CFLAGS = $(GPG_ERROR_CFLAGS)
+gchash_LDADD = libgcrypt.la $(GPG_ERROR_LIBS)
+
 mpicalc_SOURCES = mpicalc.c
 mpicalc_CFLAGS = $(GPG_ERROR_CFLAGS)
 mpicalc_LDADD = libgcrypt.la $(GPG_ERROR_LIBS)
diff --git a/src/gchash.c b/src/gchash.c
new file mode 100644
index 0000000..7a2aad6
--- /dev/null
+++ b/src/gchash.c
@@ -0,0 +1,120 @@
+/* gchash.c - Calculate hash values
+ * Copyright (C) 2013 Dmitry Eremin-Solenikov
+ *
+ * This file is part of Libgcrypt.
+ *
+ * Libgcrypt is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * Libgcrypt is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#ifdef _GCRYPT_IN_LIBGCRYPT
+# undef _GCRYPT_IN_LIBGCRYPT
+# include "gcrypt.h"
+#else
+# include <gcrypt.h>
+#endif
+
+
+void
+init_gcrypt (void)
+{
+  if (!gcry_check_version (GCRYPT_VERSION)) {
+    fputs ("libgcrypt version mismatch\n", stderr);
+    exit (2);
+  }
+
+  gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
+
+  /* Allocate a pool of 16k secure memory.  This make the secure memory
+   * available and also drops privileges where needed.  */
+  gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
+
+  gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
+
+  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
+}
+
+int
+main (int argc, char **argv)
+{
+  gcry_md_hd_t hd;
+  gcry_error_t err;
+  int algo;
+
+  init_gcrypt();
+
+  if (argc < 2 || (argv[1] && !strcmp(argv[1], "--help")))
+    {
+      fprintf (stderr, "Usage: %s <digest> <file>...\n", argv[0]);
+      return 1;
+    }
+
+  algo = gcry_md_map_name (argv[1]);
+  if (algo == GCRY_MD_NONE)
+    {
+      fprintf (stderr, "Unknown algorithm '%s'\n", argv[1]);
+      return 1;
+    }
+
+  err = gcry_md_open(&hd, algo, 0);
+  if (err)
+    {
+      fprintf (stderr, "LibGCrypt error %s/%s\n",
+          gcry_strsource (err),
+          gcry_strerror (err));
+      exit (1);
+    }
+
+  for (argv += 2; *argv; argv++)
+    {
+      FILE *fp;
+      unsigned char buf[1024];
+      size_t size;
+      int i;
+      unsigned char *h;
+      if (!strcmp (*argv, "-"))
+        fp = stdin;
+      else
+        fp = fopen (*argv, "r");
+
+      if (fp == NULL)
+        {
+          perror ("fopen");
+          return 1;
+        }
+
+      while (!feof (fp))
+        {
+          size = fread (buf, 1, sizeof(buf), fp);
+          gcry_md_write (hd, buf, size);
+        }
+
+      h  = gcry_md_read(hd, 0);
+
+      for (i = 0; i < gcry_md_get_algo_dlen (algo); i++)
+        printf("%02hhx", h[i]);
+      printf("  %s\n", *argv);
+
+      gcry_md_reset(hd);
+    }
+
+  gcry_md_close(hd);
+  return 0;
+}
-- 
1.8.4.rc3




More information about the Gcrypt-devel mailing list