swig c++ to python with openssl/bn.h
swig c++ to python with openssl/bn.h
我正在尝试创建一个 *.so 文件,以便使用 SWIG 在 Python 中进一步使用。特别是我正在使用openssl的一些库(例如opensll/bn.h)。但不知何故,它会返回错误ImportError: [...]/auxchash.so: undefined symbol: BN_bn2hex
.
我有文件.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;
}
文件.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);
swig 模块 auxchash.i 的 file.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 一个文件来创建所有需要的 swig 文件,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
到目前为止一切顺利,它编译没有错误。但是当我运行一个 python main 时:
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,例如:
auxchash_module = Extension('_auxchash',
sources=['auxchash_wrap.cxx', 'auxchash.cpp'],
libraries=['crypto', 'ssl'],
)
(您可能只需要该列表中的加密货币,我现在不太记得/告诉)
相关文章:
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 如何在openssl-ecc中获取十六进制格式的私钥
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 如何在OpenSSL中从configuration.h.in获取configuration.h
- OpenSSL TLS服务器-使用客户端证书白名单
- 通过 Openssl 命令行加密,通过 c++ 解密
- 使用已使用 java 编码的 openssl 解码数据
- OpenSSL没有共享密码
- OpenSSL 解密功能无法正常工作
- OpenSSL BIO and SSL_read
- 如何在OpenSSL库的名称中添加后缀'd'?
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Boost.Asio/OpenSSL HTTPS GET certificate trouble
- 使用 OpenSSL 从内存中读取原始 SSL/TLS 证书
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- OpenSSL: EC_POINT_set_compressed_coordinates_GFp segfault
- 在 C/C++ 中加载 OpenSSL 自定义引擎
- 我的应用程序无法在安卓上使用OpenSSL进行链接
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- swig c++ to python with openssl/bn.h