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/>.
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"
38 gint
l_quvi_crypto_hash(lua_State
*l
)
40 struct l_quvi_object_crypto_opts_s co
;
41 gboolean croak_if_error
;
46 memset(&co
, 0, sizeof(struct l_quvi_object_crypto_opts_s
));
50 q
= (_quvi_t
) l_get_reg_userdata(l
, USERDATA_QUVI_T
);
55 co
.text
= luaL_checkstring(l
, 1);
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
);
72 s
= crypto_hex2bytes(co
.text
, &n
);
76 crypto_dump("s", s
, n
);
81 l_quvi_object_crypto_chk_if_failed(l
, c
, croak_if_error
, q
);
84 l_quvi_object_crypto_invalid_hexstr(l
, q
, croak_if_error
);
87 /* Return a table of results. */
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);
100 l_quvi_object_opts_free(opts
);
103 return (1); /* no. of returned values (a table) */
106 /* vim: set ts=2 sw=2 tw=72 expandtab: */