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/

相关文章:

wcf - .Net 中的序列化内部结构

java - 序列化和反序列化的重要性

python ctypes,使用参数调用 user32 和 kernel32 func

python - 装饰器作为类

c# - 如何使用 protobuf-net 读取 fixed32 整数?

python - 如何同时监控loss和val_loss以避免神经网络对训练集或测试集过度拟合?

python - 在 python 中使用 ctypes 从 .so 文件链接时出现奇怪的改变行为

python - CPython 中的 id(obj) 和 ctypes.addressof(obj) 有什么区别

python 列表循环技术语法

python - 图像识别——寻找相似图像