How to get MD5 sum of a string using python?

Question:

In the Flickr API docs, you need to find the MD5 sum of a string to generate the [api_sig] value.

How does one go about generating an MD5 sum from a string?

Flickr’s example:

string: 000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite

MD5 sum: a02506b31c1cd46c2e0b6380fb94eb3d

Asked By: super9

||

Answers:

Have you tried using the MD5 implementation in hashlib? Note that hashing algorithms typically act on binary data rather than text data, so you may want to be careful about which character encoding is used to convert from text to binary data before hashing.

The result of a hash is also binary data – it looks like Flickr’s example has then been converted into text using hex encoding. Use the hexdigest function in hashlib to get this.

Answered By: Jon Skeet

You can do the following:

Python 2.x

import hashlib
print hashlib.md5("whatever your string is").hexdigest()

Python 3.x

import hashlib
print(hashlib.md5("whatever your string is".encode('utf-8')).hexdigest())

However in this case you’re probably better off using this helpful Python module for interacting with the Flickr API:

… which will deal with the authentication for you.

Official documentation of hashlib

Answered By: Mark Longair

For Python 2.x, use python’s hashlib

import hashlib
m = hashlib.md5()
m.update("000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite")
print m.hexdigest()

Output: a02506b31c1cd46c2e0b6380fb94eb3d

Answered By: Ikke
Try This 
import hashlib
user = input("Enter text here ")
h = hashlib.md5(user.encode())
h2 = h.hexdigest()
print(h2)
Answered By: Ome Mishra

You can Try with

#python3
import hashlib
rawdata = "put your data here"
sha = hashlib.sha256(str(rawdata).encode("utf-8")).hexdigest() #For Sha256 hash
print(sha)
mdpass = hashlib.md5(str(sha).encode("utf-8")).hexdigest() #For MD5 hash
print(mdpass)
Answered By: Md Jewele Islam

You can use b character in front of a string literal:

import hashlib
print(hashlib.md5(b"Hello MD5").hexdigest())
print(hashlib.md5("Hello MD5".encode('utf-8')).hexdigest())

Out:

e5dadf6524624f79c3127e247f04b548
e5dadf6524624f79c3127e247f04b548
Answered By: prosti

Use hashlib.md5 in Python 3.

import hashlib

source = '000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite'.encode()
md5 = hashlib.md5(source).hexdigest() # returns a str
print(md5) # a02506b31c1cd46c2e0b6380fb94eb3d

If you need byte type output, use digest() instead of hexdigest().

Answered By: z0gSh1u

simple toolkit:

In [62]: import hashlib
    ...:
    ...: def make_md5(data: str) -> str:
    ...:     md5_value = hashlib.md5(data.encode('utf-8')).hexdigest()
    ...:     return md5_value
    ...:

In [63]:

In [63]: make_md5("123-123-123-123")
Out[63]: '779e9814651491eae36438dff100820d'
Answered By: Ershan

This worked for me on windows 10:

import hashlib

print(hashlib.md5("string to encode".encode('utf-8')).hexdigest())

Answered By: Cyebukayire

If you want to get results that are easy to do arithmetic with (ie. modulo), you could try this:

import hashlib
import struct

struct.unpack('iiii', hashlib.md5(b'hi').digest())

Which yields:

(1552610889, 753701764, -2104888309, 1006379292)
Answered By: Dale Johnson
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.