Use word "library" in the source file headers
[libquvi.git] / src / lua / quvi / crypto / hash.c
blob01f4fa200b10a101f2eee02d90b3a744efef42d4
1 /* libquvi
2 * Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <lauxlib.h>
24 #include <gcrypt.h>
25 #include <glib.h>
26 #include <quvi.h>
28 #include "_quvi_s.h"
29 /* -- */
30 #include "gcrypt/crypto.h"
31 #include "lua/quvi/crypto/opts.h"
32 #include "lua/quvi/crypto/err.h"
33 #include "lua/quvi/opts.h"
34 #include "lua/getfield.h"
35 #include "lua/setfield.h"
36 #include "lua/def.h"
38 gint l_quvi_crypto_hash(lua_State *l)
40 struct l_quvi_object_crypto_opts_s co;
41 gboolean croak_if_error;
42 GSList *opts;
43 crypto_t c;
44 _quvi_t q;
46 memset(&co, 0, sizeof(struct l_quvi_object_crypto_opts_s));
48 /* quvi handle */
50 q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
51 g_assert(q != NULL);
53 /* function args */
55 co.text = luaL_checkstring(l, 1);
56 lua_pop(l, 1);
58 /* options */
60 opts = l_quvi_object_opts_new(l, 2);
61 croak_if_error = l_quvi_object_opts_croak_if_error(l, opts);
62 l_quvi_object_crypto_hash_chk_opts(l, opts, &co);
64 /* generate message digest (return as a hexstring). */
66 c = crypto_new(co.algoname, CRYPTO_MODE_HASH, NULL, -1, -1);
67 q->status.rc = l_quvi_object_crypto_chk_if_failed(l, c, croak_if_error, q);
69 guchar *s;
70 gsize n;
72 s = crypto_hex2bytes(co.text, &n);
73 if (s != NULL)
75 #ifdef _1
76 crypto_dump("s", s, n);
77 #endif
78 crypto_exec(c, s, n);
79 g_free(s);
80 q->status.rc =
81 l_quvi_object_crypto_chk_if_failed(l, c, croak_if_error, q);
83 else
84 l_quvi_object_crypto_invalid_hexstr(l, q, croak_if_error);
87 /* Return a table of results. */
89 lua_newtable(l);
90 l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1);
91 l_setfield_n(l, QO_QUVI_CODE, q->status.rc);
93 if (q->status.rc == QUVI_OK)
95 gchar *s = crypto_bytes2hex(c->out.data, c->out.dlen);
96 l_setfield_s(l, QO_DIGEST, s, -1);
97 g_free(s);
100 l_quvi_object_opts_free(opts);
101 crypto_free(c);
103 return (1); /* no. of returned values (a table) */
106 /* vim: set ts=2 sw=2 tw=72 expandtab: */