From dce3be261d53b6148e9f599e1a18aeae63e26ff4 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 18 Dec 2015 23:08:07 +1100 Subject: bignum: Add tohex function --- src/openssl.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/openssl.c b/src/openssl.c index cb74506..d6bec90 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1970,6 +1970,35 @@ sslerr: } /* bn_todec() */ +static int bn_tohex(lua_State *L) { + BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); + char *txt = NULL; + BIO *bio; + BUF_MEM *buf; + + if (!(txt = BN_bn2hex(bn))) + goto sslerr; + + /* use GC-visible BIO as temporary buffer */ + bio = getbio(L); + + if (BIO_puts(bio, txt) < 0) + goto sslerr; + + OPENSSL_free(txt); + txt = NULL; + + BIO_get_mem_ptr(bio, &buf); + lua_pushlstring(L, buf->data, buf->length); + + return 1; +sslerr: + OPENSSL_free(txt); + + return auxL_error(L, auxL_EOPENSSL, "bignum:tohex"); +} /* bn_tohex() */ + + static const luaL_Reg bn_methods[] = { { "add", &bn__add }, { "sub", &bn__sub }, @@ -1981,6 +2010,7 @@ static const luaL_Reg bn_methods[] = { { "shr", &bn__shr }, { "tobin", &bn_tobin }, { "todec", &bn_todec }, + { "tohex", &bn_tohex }, { NULL, NULL }, }; -- cgit v1.2.3-55-g6feb