-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctypes_sha256.py
29 lines (22 loc) · 957 Bytes
/
ctypes_sha256.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import ctypes
from ctypes.util import find_library
# Load the libcrypto library
libcrypto_path = find_library('crypto')
if not libcrypto_path:
raise Exception('libcrypto not found')
libcrypto = ctypes.CDLL(libcrypto_path)
# Define the function prototype
# The SHA256 function takes three parameters: the data to hash, the length of the data, and the output buffer
libcrypto.SHA256.argtypes = [ctypes.POINTER(ctypes.c_ubyte), ctypes.c_size_t, ctypes.POINTER(ctypes.c_ubyte)]
libcrypto.SHA256.restype = ctypes.POINTER(ctypes.c_ubyte)
# Prepare the data to hash
data = b"Hello, world!"
data_length = len(data)
data_array = (ctypes.c_ubyte * data_length)(*data)
# Prepare the output buffer (32 bytes for SHA-256)
buffer = (ctypes.c_ubyte * 32)()
# Call the SHA256 function
result = libcrypto.SHA256(data_array, data_length, buffer)
# Convert the result to a hexadecimal string
hex_digest = ''.join(format(x, '02x') for x in buffer)
print(hex_digest)