gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:15 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com