Add wrbuf_sha1_write
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 23 Jan 2014 14:12:32 +0000 (15:12 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 23 Jan 2014 14:12:32 +0000 (15:12 +0100)
include/yaz/wrbuf.h
src/Makefile.am
src/wrbuf_sha1.c [new file with mode: 0644]
test/test_wrbuf.c
win/makefile

index 4db046c..8664c36 100644 (file)
@@ -274,6 +274,17 @@ void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd,
 YAZ_EXPORT
 void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz);
 
+/** \brief writes SHA1 text to WRBUF
+    \param b result
+    \param cp char buffer
+    \param sz size of char buffer
+    \param hexit 1=hex mode; 0=binary
+    \returns 0 if successful
+    \returns -1 on error
+*/
+YAZ_EXPORT
+int wrbuf_sha1_write(WRBUF b, const char *cp, size_t sz, int hexit);
+
 YAZ_END_CDECL
 
 #endif
index ad34fa6..5ccb53f 100644 (file)
@@ -71,11 +71,11 @@ GEN_FILES = oid_std.c \
   z-charneg.c \
   ill-core.c item-req.c oclc-ill-req-ext.c
 
-libyaz_la_SOURCES=base64.c version.c options.c log.c \
- $(GEN_FILES) \
+libyaz_la_SOURCES= $(GEN_FILES) \
+  base64.c version.c options.c log.c \
   cookie.c marcdisp.c \
   marc_read_json.c marc_read_xml.c marc_read_iso2709.c marc_read_line.c \
-  wrbuf.c oid_db.c errno.c \
+  wrbuf.c wrbuf_sha1.c oid_db.c errno.c \
   nmemsdup.c xmalloc.c readconf.c tpath.c nmem.c matchstr.c atoin.c \
   siconv.c iconv-p.h utf8.c ucs4.c iso5428.c advancegreek.c \
   odr_bool.c ber_bool.c ber_len.c ber_tag.c odr_util.c facet.c \
diff --git a/src/wrbuf_sha1.c b/src/wrbuf_sha1.c
new file mode 100644 (file)
index 0000000..9412d1d
--- /dev/null
@@ -0,0 +1,60 @@
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) Index Data
+ * See the file LICENSE for details.
+ */
+
+/**
+ * \file wrbuf_sha1.c
+ * \brief Implements SHA1 creation over WRBUF
+ */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <yaz/wrbuf.h>
+#if HAVE_GCRYPT_H
+#include <gcrypt.h>
+#endif
+
+int wrbuf_sha1_write(WRBUF b, const char *cp, size_t sz, int hexit)
+{
+#if HAVE_GCRYPT_H
+    gcry_error_t e;
+    gcry_md_hd_t hd;
+    const unsigned char *digest_buf;
+    int digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
+
+    e = gcry_md_open(&hd, GCRY_MD_SHA1, 0);
+    if (e)
+        return -1;
+    gcry_md_write(hd, cp, sz);
+
+    digest_buf = gcry_md_read(hd, GCRY_MD_SHA1);
+    if (hexit)
+    {
+        int i;
+        for (i = 0; i < digest_len; i++)
+            wrbuf_printf(b, "%02x", digest_buf[i]);
+    }
+    else
+        wrbuf_write(b, (const char *) digest_buf, digest_len);
+    gcry_md_close(hd);
+    return 0;
+#else
+    return -1;
+#endif
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
index 644f52a..e7886c4 100644 (file)
@@ -76,6 +76,15 @@ static void tstwrbuf(void)
     wrbuf_insert(wr, 5, "abc", 3);
     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1234"));
 
+#if HAVE_GCRYPT_H
+    {
+        const char *msg = "Hello world\n";
+        wrbuf_rewind(wr);
+        wrbuf_sha1_write(wr, msg, strlen(msg), 1);
+        YAZ_CHECK(!strcmp(wrbuf_cstr(wr), 
+                          "33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d"));
+    }
+#endif
     wrbuf_destroy(wr);
 }
 
index ac38182..401b7e4 100644 (file)
@@ -457,6 +457,7 @@ MISC_OBJS= \
    $(OBJDIR)\readconf.obj \
    $(OBJDIR)\tpath.obj \
    $(OBJDIR)\wrbuf.obj \
+   $(OBJDIR)\wrbuf_sha1.obj \
    $(OBJDIR)\xmalloc.obj \
    $(OBJDIR)\matchstr.obj \
    $(OBJDIR)\siconv.obj \