X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fwrbuf_sha1.c;fp=src%2Fwrbuf_sha1.c;h=9412d1db4789d25a2ec17c7f29a7f823902fb0c1;hp=0000000000000000000000000000000000000000;hb=7efed25c2ed807d32001e749f87f2eb300548c22;hpb=23a11d4150aeef79e2583c964214db8ad428487a 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 + */