2017-10-18 21 views
1

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 

합니다.

+0

(당신은 해당 목록에 암호화를해야 할 수도 있습니다, 나는 확실히 말할 지금/기억이 안나요) 빠른 읽기, 나는 함수가 호출자의 포인터를 변경할 수 있도록'int keygen (int 비트, char ** p, char **, char **, char ** hk, char ** tk)'이 필요하다고 생각합니다. . '* p = BN_bn2hex (bn_p)'와 같은 것. – jww

+0

'auxchash.so : undefined symbol : BN_bn2hex'에 대해서'ldd auxchash.so'는 무엇을 보여줍니까? 'libcrypto.so'에 의존성이 있습니까? – jww

+0

예 @jww 네 말이 맞아, 쓴 것은 절대적으로 옳다. 그러나 나는 swig와 char **로도 어려움을 겪었습니다. 나는 swig에서 char *에 대한 이중 포인터를 처리 할 수 ​​없으므로이 접근 방식을 채택하기로 결정했습니다. 이 [link] (https://stackoverflow.com/questions/46776757/swig-char-as-a-pointer-to-a-char? noredirect=1#comment80538743_46776757)를 참조하십시오. – lorsp000

답변

0

당신이 좋아 egsomething, OpenSSL에 대한 모듈을 연결해야합니다

auxchash_module = Extension('_auxchash', 
         sources=['auxchash_wrap.cxx', 'auxchash.cpp'], 
         libraries=['crypto', 'ssl'], 
         ) 

가에

+0

대단히 감사합니다! 네, libraries 매개 변수를 추가하면 작동하는 것 같습니다. (: – lorsp000

+0

그것은 작동하지만 지금 다른 문제가 생깁니다. 파이썬 메인에 반환 변수를 인쇄하면 해독 할 수없는 문자가 인쇄됩니다. 반면에 C++ 함수 내에서 동일한 값을 인쇄하면 올바른 값을 얻습니다. 스윙 모듈의 출력에 문제가 있습니다. 그러나'% cstring_bounded_output'는 char *을 올바르게 반환해야합니다.이 링크가 라이브러리에 연결되어 있습니까? – lorsp000

+0

나는 더 나은 이해를 위해 리턴 변수에 간단한 문자열 "hello"를 쓰려고 노력했다. 파이썬 메인이 올바르게 가져올 수 없습니다. (즉, 여전히 해독 할 수없는 문자를 출력합니다.) – lorsp000