diff options
author | daurnimator <quae@daurnimator.com> | 2015-12-18 23:08:07 +1100 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2015-12-21 19:32:54 +1100 |
commit | dce3be261d53b6148e9f599e1a18aeae63e26ff4 (patch) | |
tree | cafc9115145cdd1e18fe485197a2d52828a6f0fa | |
parent | e1ccaebacd4e8f277222d2177e7c02fb31ea0bf2 (diff) | |
download | luaossl-dce3be261d53b6148e9f599e1a18aeae63e26ff4.tar.gz luaossl-dce3be261d53b6148e9f599e1a18aeae63e26ff4.tar.bz2 luaossl-dce3be261d53b6148e9f599e1a18aeae63e26ff4.zip |
bignum: Add tohex function
-rw-r--r-- | src/openssl.c | 30 |
1 files changed, 30 insertions, 0 deletions
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 }, }; |