[PATCH v2] Add an utility to calculate hashes over a set of files
Dmitry Eremin-Solenikov
dbaryshkov at gmail.com
Sun Dec 22 14:13:45 CET 2013
* tests/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>
---
.gitignore | 1 +
tests/Makefile.am | 3 +-
tests/gchash.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 123 insertions(+), 1 deletion(-)
create mode 100644 tests/gchash.c
diff --git a/.gitignore b/.gitignore
index ec7f8bb..8b235f9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -69,6 +69,7 @@ tests/basic
tests/benchmark
tests/fips186-dsa
tests/fipsdrv
+tests/gchash
tests/hmac
tests/keygen
tests/keygrip
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f5b5b9f..46d2d81 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -48,7 +48,8 @@ AM_CFLAGS = $(GPG_ERROR_CFLAGS)
LDADD = ../src/libgcrypt.la $(DL_LIBS) ../compat/libcompat.la $(GPG_ERROR_LIBS)
EXTRA_PROGRAMS = testapi pkbench
-noinst_PROGRAMS = $(tests_bin) $(tests_bin_last) fipsdrv rsacvt genhashdata
+noinst_PROGRAMS = $(tests_bin) $(tests_bin_last) fipsdrv rsacvt genhashdata \
+ gchash
EXTRA_DIST = README rsa-16k.key cavs_tests.sh cavs_driver.pl \
pkcs1v2-oaep.h pkcs1v2-pss.h pkcs1v2-v15c.h pkcs1v2-v15s.h \
diff --git a/tests/gchash.c b/tests/gchash.c
new file mode 100644
index 0000000..7a2aad6
--- /dev/null
+++ b/tests/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.5.1
More information about the Gcrypt-devel
mailing list