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
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 \
--- /dev/null
+/* 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
+ */
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);
}
$(OBJDIR)\readconf.obj \
$(OBJDIR)\tpath.obj \
$(OBJDIR)\wrbuf.obj \
+ $(OBJDIR)\wrbuf_sha1.obj \
$(OBJDIR)\xmalloc.obj \
$(OBJDIR)\matchstr.obj \
$(OBJDIR)\siconv.obj \