I am trying to use a function from a C library in python using ctypes
. The called function calculates a SHA256 hash output
from the input
of fixed length 38
and a given state
of the hash function.
The problem now is, that I get different results calling the function with python and calling it within a C program. Is there a difference in encoding of uint8
of C and byte-array strings from python? Or am I missing something else?
Here the function call in C:
uint8_t input[38] = {"0"};
uint8_t state[40] = {"0"};
uint8_t output[32] = {"0"};
sha256_inc_finalize(output, state, input, 38);
And the function call in python (3.10.4):
import ctypes
sha = ctypes.CDLL('/path/to/lib/sha_lib.so')
func = sha.sha256_inc_finalize
func.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t]
out = ctypes.create_string_buffer(b'00000000000000000000000000000000', size=32)
state = ctypes.create_string_buffer(b'0000000000000000000000000000000000000000', size=40)
input = b'00000000000000000000000000000000000000'
func(out, state, input, 38)
I do not include the value of output
after the call of sha256_inc_finalize
here because it is just a hash value.
Any clue on what's going on here is much appreciated!
EDIT:
The hash of the function called from C is
3ca2f9f4 37712b50 7b046090 4497276a 81199415 1a7760a3 60840c92 747c8bbe
while the call via python outputs:
133cc1c1 4a8bd095 37e022d1 829a2261 1a0aab38 35a0c2ab 291a088e bb2d1c82