thmasker
thmasker

Reputation: 638

Python 3 Create md5 hash

I must decode the following data:

b'E\x00\x00 <\xed\x00\x00>\x01\x15\xe2\xac\x140f\xa1C\xacP\x00\x00\xf8V\x00\x07\x00\x07\x00\x01\x07\x9a'

into an understandable string. For that, we were told to use hashlib and md5. But I don't know how to use it to decypher this message.

I've tried something like this:

message.hashlib().md5().decode()

But I do not obtain any result

Upvotes: 5

Views: 28910

Answers (2)

Tigran Nalbandyan
Tigran Nalbandyan

Reputation: 75

I suggest you to read The Official Documentation of hashlib Documentation.

Simple example:

import hashlib

text = 'Some text 2'

m = hashlib.md5()
m.update(b"Some text") # OR
m.update(text.encode('UTF-8'))
print(m.hexdigest())

Upvotes: 3

tdelaney
tdelaney

Reputation: 77337

You can't get there from here. A hash is a small refactoring of data that destroys virtually all of the information in the data. It is used to identify a revision of the data and can be used later to see if the data has changed. A good hash algorithm changes its output dramatically with even a 1 character change in the data. Consider a Midsummer Night's Dream on gutenberg.org. Its about 100,000 characters and its md5 hash is 16 bytes. You are not going to get the original back from that!

>>> import hashlib
>>> import requests
>>> night = requests.get("http://www.gutenberg.org/ebooks/1514.txt.utf-8")
>>> len(night.text)
112127

>>> print(night.text[20000:20200])
h power to say, Behold!
The jaws of darkness do devour it up:
So quick bright things come to confusion.

HERMIA
If then true lovers have ever cross'd,
It stands as an edict in destiny:
Then let
>>> print(night.text[20000:20300])
h power to say, Behold!
The jaws of darkness do devour it up:
So quick bright things come to confusion.

HERMIA
If then true lovers have ever cross'd,
It stands as an edict in destiny:
Then let us teach our trial patience,
Because it is a customary cross;
As due to love as thoughts, and dre

>>> hash = hashlib.md5(night.text.encode("utf-8")).hexdigest()
>>> print(hash)
cce0d35b8b2c4dafcbde3deb983fec0a

The hash can be very useful to see if the text has changed:

>>> hash2 = hashlib.md5(requests.get("http://www.gutenberg.org/ebooks/1514.txt.utf-8").text.encode("utf-8")).hexdigest()
>>> hash == hash2
True

Upvotes: 10

Related Questions