SWIG를 사용하여 파이썬에서 사용하기 위해 * .so 파일을 만들려고합니다. 특히 openssl (opensll/bn.h)의 일부 라이브러리를 사용하고 있습니다. 하지만 여하튼 그것은 오류 ImportError: [...]/auxchash.so: undefined symbol: BN_bn2hex
을 반환합니다.swig C++에서 openssl/bn.h를 사용하여 파이썬에
#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);
꿀꺽 꿀꺽 모듈 믿을수, auxchash.i
은 내가 file.cpp, auxchash.cpp이 마지막으로
%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 :그리고 그들 모두은 터미널 명령으로 컴파일 :
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
합니다.
(당신은 해당 목록에 암호화를해야 할 수도 있습니다, 나는 확실히 말할 지금/기억이 안나요) 빠른 읽기, 나는 함수가 호출자의 포인터를 변경할 수 있도록'int keygen (int 비트, char ** p, char **, char **, char ** hk, char ** tk)'이 필요하다고 생각합니다. . '* p = BN_bn2hex (bn_p)'와 같은 것. – jww
'auxchash.so : undefined symbol : BN_bn2hex'에 대해서'ldd auxchash.so'는 무엇을 보여줍니까? 'libcrypto.so'에 의존성이 있습니까? – jww
예 @jww 네 말이 맞아, 쓴 것은 절대적으로 옳다. 그러나 나는 swig와 char **로도 어려움을 겪었습니다. 나는 swig에서 char *에 대한 이중 포인터를 처리 할 수 없으므로이 접근 방식을 채택하기로 결정했습니다. 이 [link] (https://stackoverflow.com/questions/46776757/swig-char-as-a-pointer-to-a-char? noredirect=1#comment80538743_46776757)를 참조하십시오. – lorsp000