Use WRBUF for addinfo member of cql_transform_t
[yaz-moved-to-github.git] / src / wrbuf_sha1.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file wrbuf_sha1.c
8  * \brief Implements SHA1 creation over WRBUF
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <yaz/wrbuf.h>
20 #if HAVE_GCRYPT_H
21 #include <gcrypt.h>
22 #endif
23
24 int wrbuf_sha1_write(WRBUF b, const char *cp, size_t sz, int hexit)
25 {
26 #if HAVE_GCRYPT_H
27     gcry_error_t e;
28     gcry_md_hd_t hd;
29     const unsigned char *digest_buf;
30     int digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
31
32     e = gcry_md_open(&hd, GCRY_MD_SHA1, 0);
33     if (e)
34         return -1;
35     gcry_md_write(hd, cp, sz);
36
37     digest_buf = gcry_md_read(hd, GCRY_MD_SHA1);
38     if (hexit)
39     {
40         int i;
41         for (i = 0; i < digest_len; i++)
42             wrbuf_printf(b, "%02x", digest_buf[i]);
43     }
44     else
45         wrbuf_write(b, (const char *) digest_buf, digest_len);
46     gcry_md_close(hd);
47     return 0;
48 #else
49     return -1;
50 #endif
51 }
52
53 int wrbuf_sha1_puts(WRBUF b, const char *cp, int hexit)
54 {
55     return wrbuf_sha1_write(b, cp, strlen(cp), hexit);
56 }
57
58 /*
59  * Local variables:
60  * c-basic-offset: 4
61  * c-file-style: "Stroustrup"
62  * indent-tabs-mode: nil
63  * End:
64  * vim: shiftwidth=4 tabstop=8 expandtab
65  */