有人知道如何使用 ctypes 公开 python 2.x _hashlib.pyd 内部结构吗?我特别需要提取 EVP_MD_CTX 结构来序列化 python HASH 对象。
最佳答案
从头文件(openssl/evp.h 和 _hashopenssl.c 在您的例子中)映射 C 结构很简单,但并不总是可以跨不同版本移植。这是我的环境:
from ctypes import *
PyObject_HEAD = [
('ob_refcnt', c_size_t),
('ob_type', c_void_p),
]
class EVP_MD(Structure):
_fields_ = [
('type', c_int),
('pkey_type', c_int),
('md_size', c_int),
('flags', c_ulong),
('init', c_void_p),
('update', c_void_p),
('final', c_void_p),
('copy', c_void_p),
('cleanup', c_void_p),
('sign', c_void_p),
('verify', c_void_p),
('required_pkey_type', c_int*5),
('block_size', c_int),
('ctx_size', c_int),
]
class EVP_MD_CTX(Structure):
_fields_ = [
('digest', POINTER(EVP_MD)),
('engine', c_void_p),
('flags', c_ulong),
('md_data', POINTER(c_char)),
]
class EVPobject(Structure):
_fields_ = PyObject_HEAD + [
('name', py_object),
('ctx', EVP_MD_CTX),
]
下面是如何使用它来 save and restore state of hash object 的示例:
import hashlib
hash = hashlib.md5('test')
print hash.hexdigest()
c_evp_obj = cast(c_void_p(id(hash)), POINTER(EVPobject)).contents
ctx = c_evp_obj.ctx
digest = ctx.digest.contents
state = ctx.md_data[:digest.ctx_size]
hash2 = hashlib.md5()
c_evp_obj = cast(c_void_p(id(hash2)), POINTER(EVPobject)).contents
ctx = c_evp_obj.ctx
digest = ctx.digest.contents
memmove(ctx.md_data, state, digest.ctx_size)
print hash2.hexdigest()
关于python - 公开 EVP_MD_CTX 的 _hashlib.pyd 内部结构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880456/