swig c + + python与openssl/bn.h
问题描述:
我想创建一个* .so文件,以便在使用SWIG的Python中进一步使用。特别是我使用了openssl的一些库(例如opensll/bn.h)。但不知何故,它返回错误ImportError: [...]/auxchash.so: undefined symbol: BN_bn2hex
。swig c + + python与openssl/bn.h
我有file.cpp,auxchash.cpp:
#include auxchash.h
int keygen(int bits, char *p, char *q, char *g, char *hk, char *tk){
BN_CTX *ctx = BN_CTX_new();
BIGNUM *bn_p = BN_new();
BIGNUM *bn_q = BN_new();
BIGNUM *bn_g = BN_new();
BIGNUM *bn_hk = BN_new();
BIGNUM *bn_tk = BN_new();
BIGNUM *bn_two = BN_new();
BN_CTX_init(ctx);
BN_dec2bn(&bn_two, "2"); //initialize a BIGNUM with value 2
//on non-unix platform needs to initialize the PRNG with randomness
//or BN_generate_prime_ex may fail
//computing the safe prime p and q = (p-1)/2
BN_generate_prime_ex(bn_p, bits, 1, NULL, NULL, NULL);
BN_sub(bn_q, bn_p, BN_value_one());
BN_div(bn_q, NULL, bn_q, bn_two, ctx);
//finding the generator g (for the group QR_p)
BN_rand_range(bn_g, bn_p);
BN_mod_exp(bn_g, bn_g, bn_two, bn_p, ctx);
//choosing the keys hk and tk
BN_rand_range(bn_tk, bn_q);
BN_mod_exp(bn_hk, bn_g, bn_tk, bn_p, ctx);
//converting from BIGNUM to hex
p = BN_bn2hex(bn_p);
q = BN_bn2hex(bn_q);
g = BN_bn2hex(bn_g);
hk = BN_bn2hex(bn_hk);
tk = BN_bn2hex(bn_tk);
//freeing the resources
BN_CTX_free(ctx);
BN_free(bn_two);
BN_free(bn_p);
BN_free(bn_q);
BN_free(bn_g);
BN_free(bn_hk);
BN_clear_free(bn_tk);
return 0;
}
的file.h,auxchash.h:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<openssl/bn.h>
#include<openssl/sha.h>
#include<openssl/rand.h>
int keygen(int bits, char *p, char *q, char *g, char *hk, char *tk);
为痛饮模块file.i,auxchash.i :
%module auxchash
%{
#define SWIG_FILE_WITH_INIT
#include "auxchash.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<openssl/bn.h>
#include<openssl/sha.h>
#include<openssl/rand.h>
%}
%include "typemaps.i"
%include "cstring.i"
%cstring_bounded_output(char *p, 1024);
%cstring_bounded_output(char *q, 1024);
%cstring_bounded_output(char *g, 1024);
%cstring_bounded_output(char *hk, 1024);
%cstring_bounded_output(char *tk, 1024);
extern int keygen(int bits, char *p, char *q, char *g, char *hk, char *tk);
最后一个文件setup.py创建所有需要的文件痛饮,setup.py:
from distutils.core import setup, Extension
auxchash_module = Extension('_auxchash',
sources=['auxchash_wrap.cxx', 'auxchash.cpp'],
)
setup (name = 'auxchash',
version = '0.1',
author = "SWIG Docs",
description = """Simple swig example from docs""",
ext_modules = [auxchash_module],
py_modules = ["auxchash"],
)
而且所有的人都编译终端命令:
swig -c++ -python auxchash.i
python setup.py build_ext --inplace
到目前为止好,它编译没有错误。但后来当我运行主蟒蛇:
import auxchash
res,p,q,g,hk,tk = auxchash.keygen(10)
它给我以下错误:
File: "[...]/auxchash.py" import auxchash
File: "[...]/auxchash.py" auxchash=swig_import_helper()
File: "[...]/auxchash.py" return=importlib.import_module('_auxchash')
File: "[...]/__init.py__" __import__(name)`
ImportError: [...]/auxchash.so: undefined symbol: BN_bn2hex
我不知道如何计算出来。
答
您需要将自己对OpenSSL的模块,像egsomething:
auxchash_module = Extension('_auxchash',
sources=['auxchash_wrap.cxx', 'auxchash.cpp'],
libraries=['crypto', 'ssl'],
)
(您可能只需要密码在该列表中,我不太记得了/你能告诉现在)
在一个快速阅读,我认为你需要'int keygen(int bits,char ** p,char ** q,char ** g,char ** hk,char ** tk)''所以函数可以改变调用者的指针。类似'* p = BN_bn2hex(bn_p)'。 – jww
关于'auxchash.so:未定义的符号:BN_bn2hex','ldd auxchash.so'显示了什么?是否存在对'libcrypto.so'的依赖? – jww
是@jww你是对的,你写的是绝对正确的。但即使使用swig和char **,我也遇到了麻烦。我决定采用这种方法,因为我无法处理swig中char *的双指针。看到这[链接](https://stackoverflow.com/questions/46776757/swig-char-as-a-pointer-to-a-char?noredirect=1#comment80538743_46776757)。 – lorsp000