Convert Delphi BufferToHex function to Python -


i learning python. apologies in advance.

i have delphi function creates string hash. first converts string elf hash(??) changes hex number.

i have first part working in python

def elfhash(key):     hash = 0     x    = 0     in range(len(key)):       hash = (hash << 4) + ord(key[i])       x = hash & 0xf0000000       if x != 0:         hash ^= (x >> 24)       hash &= ~x     return hash 

in delphi additional step done converts hex value

function buffertohex(const buf; bufsize : cardinal) : string; var       : longint; begin   result := '';   := 0 bufsize - 1     result := result + inttohex(tbytearray(buf)[i], 2);  end; 

buf here elf hash got, stored in delphi longint , bufsize delphi sizeof() of longint, far seems return 4.

how make python function similar buffertohex 1 return equivalent output? far can tell python types different , not returning same byte size (it seems return 16 not 4), , when messed ctypes stuff still getting different numbers.

any advice appreciated. thanks.

all function convert binary hex string. since input hash 32 bits wide need this:

'%08x' % hash 

where hash int contains hashed value.

since guess on little endian machine, have hex bytes reversed. fix this:

hashstr = '%08x' % hash hashstr = "".join(reversed([hashstr[i:i+2] in range(0, len(hashstr), 2)])) 

put , have this:

def elfhash(key):     hash, x = 0, 0     in range(len(key)):         hash = (hash << 4) + ord(key[i])         x = hash & 0xf0000000         if x != 0:             hash ^= (x >> 24)             hash &= ~x     hashstr = '%08x' % hash     return "".join(reversed([hashstr[i:i+2] in range(0, len(hashstr), 2)])) 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -