From 7efed25c2ed807d32001e749f87f2eb300548c22 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 23 Jan 2014 15:12:32 +0100 Subject: [PATCH 1/1] Add wrbuf_sha1_write --- include/yaz/wrbuf.h | 11 ++++++++++ src/Makefile.am | 6 +++--- src/wrbuf_sha1.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_wrbuf.c | 9 ++++++++ win/makefile | 1 + 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/wrbuf_sha1.c diff --git a/include/yaz/wrbuf.h b/include/yaz/wrbuf.h index 4db046c..8664c36 100644 --- a/include/yaz/wrbuf.h +++ b/include/yaz/wrbuf.h @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index ad34fa6..5ccb53f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 0000000..9412d1d --- /dev/null +++ b/src/wrbuf_sha1.c @@ -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 +#endif + +#include +#include +#include + +#include +#if HAVE_GCRYPT_H +#include +#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 + */ diff --git a/test/test_wrbuf.c b/test/test_wrbuf.c index 644f52a..e7886c4 100644 --- a/test/test_wrbuf.c +++ b/test/test_wrbuf.c @@ -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); } diff --git a/win/makefile b/win/makefile index ac38182..401b7e4 100644 --- a/win/makefile +++ b/win/makefile @@ -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 \ -- 1.7.10.4