python - 公开 EVP_MD_CTX 的 _hashlib.pyd 内部结构吗?

标签 python serialization ctypes hashlib

有人知道如何使用 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/

相关文章:

python - 递归计算表达式python

python - 在 PyQt 中向 QTableWidget 单元格添加多个小部件

python - 为什么在使用 Python 2.7.11 IDLE 时无法保存中文文件?

javascript - 将工作的 JavaScript 对象序列化为仅保留属性的 JSON?

c# - 显式检索 XML 元素的内容作为字符串

python - ctypes.create_string_buffer Python 2 与 Python 3

python - 返回结构时使用 ctypes 的段错误 - 32 位 linux

Python ctypes 如何跳过可选参数

python - 谷歌应用引擎 : upload throws "TemplateDoesNotExist: index.html" error

python - Django REST 框架——Many2Many/Through