问题描述
我正在通过使用OpenSSL和MD5制作一个哈希示例来尝试C的Python API(python3),但是尝试在python3中导入模块时遇到错误。这是我的C源代码:
-
hashmodule.c
#include <stdlib.h>
#include <string.h>
#include <Python.h>
#include <openssl/md5.h>
unsigned char * __c_md5hash(const /*unsigned*/ char * string) {
unsigned char * raw_hash = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
memset(raw_hash,'\0',MD5_DIGEST_LENGTH);
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx,string,strlen(string));
MD5_Final(raw_hash,&ctx);
return raw_hash;
}
static PyObject * md5hash(PyObject * self,PyObject * args) {;
const /*unsigned*/ char * string;
if (!PyArg_ParseTuple(args,"s",&string))
return NULL;
return Py_BuildValue("s",__c_md5hash(string));
}
static PyMethodDef hashmethods[] = {
{"md5hash",md5hash,METH_VARARGS,NULL},{NULL,NULL,NULL}
};
static struct PyModuleDef hashmodule = {
PyModuleDef_HEAD_INIT,"hashmodule","a simple test hashing module for python3",-1,hashmethods
};
PyMODINIT_FUNC PyInit_testmodule(void) {
return PyModule_Create(&hashmodule);
}
这是我的python3安装脚本
-
setup.py
from distutils.core import setup,Extension
import sysconfig
setup(
name="hashmodule",version="1.0",ext_modules=[
Extension(
"hashmodule",sources=["hashmodule.c"],extra_compile_args=["-lcrypto","-lssl"]
)
]
)
如setup.py
文件所示,我为openssl添加了-lssl
和-lcrypto
标志,并且当我运行python3 setup.py build
和{ {1}}。输出为:
-
python3 setup.py install
python3 setup.py build
-
running build running build_ext building 'hashmodule' extension x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-s trong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security - Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.8 -c hashmodule.c -o build/temp.linux-x86_64-3.8/hashmod ule.o -lcrypto -lssl x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,relro -g - fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.l inux-x86_64-3.8/hashmodule.o -o build/lib.linux-x86_64-3.8/hashmodule.cpython-38-x86_64-linux-gnu.so
python3 setup.py install
如内部版本所示,它包含使用openssl进行常规编译所需的正确crypto和ssl标志。但是,当我然后进入python3并执行以下命令时,会出现此错误:
-
running install running build running build_ext running install_lib copying build/lib.linux-x86_64-3.8/hashmodule.cpython-38-x86_64-linux-gnu.so -> /usr/local/lib/python3.8/dist-packag es running install_egg_info Writing /usr/local/lib/python3.8/dist-packages/hashmodule-1.0.egg-info
python3
我已经运行>>> import hashmodule
Traceback (most recent call last):
File "<stdin>",line 1,in <module>
ImportError: /usr/local/lib/python3.8/dist-packages/hashmodule.cpython-38-x86_64-linux-gnu.so: undefined symbol: MD5_Final
>>>
来查找模块MD5功能是否包含在显示以下内容的模块中:
-
nm
nm -S /usr/local/lib/python3.8/dist-packages/hashmodule.cpython-38-x86_64-linux-gnu.so | grep MD5
任何帮助将不胜感激,如果我错过了任何事情,请告诉我。
非常感谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)