访问 C 库的最 Python 方式是什么 - 例如 OpenSSL?
我需要访问 OpenSSL 的加密函数来对 CBC 流中的 Blowfish 数据进行编码。 我在谷歌上搜索并找到了一些 Blowfish 库(手写)和一些 OpenSSL 包装器(似乎都不完整)。
最后,我需要访问某些 OpenSSL 函数,例如 命令。 访问它们的 pythonic/正确方法是什么? 使用 SWIG 之类的东西来允许 Python/C 绑定,或者有更好的方法吗?
谢谢!
I need to access the crypto functions of OpenSSL to encode Blowfish data in a CBC streams. I've googled and found some Blowfish libraries (hand written) and some OpenSSL wrappers (none of the seem complete.)
In the end, I need to access the certain OpenSSL functions, such as the full blowfish.h library of commands. What's the pythonic/right way of accessing them? Using something like SWIG to allow Python/C bindings, or is there a better way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Python 中与 C(和 C++)交互的方法有很多。 ctypes 对于快速的小扩展来说非常好,但它有将编译时错误转变为运行时段错误的习惯。 如果您想编写自己的扩展,SIP 非常好。 SWIG 非常通用,但拥有更多的追随者。 当然,您应该做的第一件事是看看您是否真的需要接口。 你看过 PyCrypto 吗?
There's lots of ways to interface with C (and C++) in Python. ctypes is pretty nice for quick little extensions, but it has a habit of turning would be compile time errors into runtime segfaults. If you're looking to write your own extension, SIP is very nice. SWIG is very general, but has a larger following. Of course, the first thing you should be doing is seeing if you really need to interface. Have you looked at PyCrypto?
ctypes 是起点。 它允许您调用 DLL、使用 C 声明的类型等。我不知道是否存在限制使您无法执行所需的所有操作,但它非常强大,并且包含在标准库中。
ctypes is the place to start. It lets you call into DLLs, using C-declared types, etc. I don't know if there are limitations that will keep you from doing everything you need, but it's very capable, and it's included in the standard library.
我对河豚的 M2Crypto(OpenSSL 包装器)感到满意。
I was happy with M2Crypto (an OpenSSL wrapper) for blowfish.
SWIG 几乎是规范的方法。 效果也很好。
SWIG is pretty much the canonical method. Works good, too.
我在 Cython 方面也取得了很好的成功。
I've had good success with Cython, as well.
我也会推荐 M2Crypto ,但是如果 joeforker 的代码示例看起来有点奇怪,您可能会遇到更轻松地理解 M2Crypto 密码单元测试,其中包括 Blowfish。 查看 test_evp.py 中的 CipherTestCase。
I would recommend M2Crypto as well, but if the code sample by joeforker looks a bit strange you might have an easier time understanding the M2Crypto cipher unit tests, which include Blowfish. Check out the CipherTestCase in test_evp.py.