1

How i should pass the value of string x (\0x02...) to the hashlib.md5 function and get the same output as result0 (05db8f903dc9adc5874eb2cab3aa725b) ?

enter image description here

Note: b'' is a prefix, that causes the following string to be interpreted as a bytes-type object. The bytes function takes a string and returns a bytes object.

This is my code:

import hashlib

result0 = hashlib.md5(b'\x02\x6d\x79\x5f\x70\x61\x73\x73\xe2\x5b\xc4\x1c\x40\x8a\x54\x84\x3e\xbd\xee\xab\xdc\x69\x90\xc0')
print(result0.hexdigest())


x= '\x02\x6d\x79\x5f\x70\x61\x73\x73\xe2\x5b\xc4\x1c\x40\x8a\x54\x84\x3e\xbd\xee\xab\xdc\x69\x90\xc0'
yourstring = x.encode('ascii', 'ignore')
var = bytes(yourstring)
result1 = hashlib.md5(var)
print(result1.hexdigest())
sjakobi
  • 3,546
  • 1
  • 25
  • 43
vraihack
  • 31
  • 5
  • This can help to go from bytes to str and viceversa: https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3 – Caridorc Jan 28 '23 at 11:20
  • 1
    @Caridorc my code already have byte encode, and my code work, but this is not my question. Please read carefully my question, if still not clear let me know. (Check the output in the image and why result0 # result1) i want to adapt the second part of my code to bring similar output as result0 1nd not the reverse – vraihack Jan 28 '23 at 21:15
  • Can someone help, i am looking for a solution for my question nearly 3 days ago and that blocking my project. thanks for your helps – vraihack Jan 29 '23 at 03:25
  • 3
    Encoding the string as `'latin-1'` instead of `'ascii'` seems to work in this case, but I'm not sure it's guaranteed under all circumstances. If you have arbitrary byte values, it should *never* be in the form of a `str` at all; there is a REASON why Python has a separate `bytes` type, you need to use that from the start. – jasonharper Jan 29 '23 at 03:41
  • 1
    @jasonharper thank you so much, it work , i was testing (ascii and utf...). i am actually doing a pythonic way for this bash command : "echo -n -e "\x02\x...." | md5sum. and these \x02\... am extracting them from a response packet i captured on wireshark – vraihack Jan 29 '23 at 04:09

0 Answers0