How to get MD5 sum of a string using python?
Asked Answered
B

10

394

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

Babbler answered 14/3, 2011 at 10:38 Comment(1)
In case anyone is wondering, you do not need to install this (and library in PyPI is very old), it is now a part of the standard libraries: docs.python.org/3/library/hashlib.htmlOutfitter
P
297

For Python 2.x, use python's hashlib

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

Output: a02506b31c1cd46c2e0b6380fb94eb3d

Polygnotus answered 14/3, 2011 at 10:43 Comment(8)
Don't try to use hashlib.md5().update('your string').hexdigest(), it won't work since update() returns None. If you want a one line solution, use Mark Longair's answer.Prizefight
@ChristopherManning m.hexdigest() returns a 32 character long digest. How to get a 16 character long digest?Betts
@Darwesh it's 32 characters because it's the hex representation, do a m.digest_size on top of this code, internal digest is already 16 bytes.Genovese
@Darwesh you can simply slice the string m.hexdigest()[:16]Geosynclinal
@Darwesh According to RFC 1321, the md5 is always 16 bytes. If you just want a 16 character long digest, you can do a slice as Baris Demiray said.Commander
work like charm, i used it to generate my hash password on dbAnkylosis
@Ankylosis It's not recmmeded to use MD5 for password hashes nowadays. THey can be bruteforced very fast, so it offers little protection.Polygnotus
The correct way in Python 3.7 is m.update( "text".encode('utf-8') ) otherwise an error will come out!Tester
W
795

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

Wigwam answered 14/3, 2011 at 10:42 Comment(6)
hexdigest() returns a 32 character long digest. How to get a 16 character long digest?Betts
Nice answer! May I ask why in Python 2 we don't need to do utf-8 encoding, however in Python 3 we need to do the encoding. Thanks. @Mark LongairFattal
@JeffHu, because hashlib.md5 expects a bytes-like-objectVivianviviana
The Python 3 version should be used in Python 2 as well. @JeffHu expanding on what @MaxU said, the md5 function takes a bytestring and does not accept unicode. Python 3 is (correctly) strict/explicit, and so a an str ("") is unicode and has to be encoded to a bytestring. Strings in python2 can be interpreted as either a btyestring or unicode string, and passing a str ("") string is interpreted as a bytestring. If the string has unicode characters, this will raise an Exception. Encoding a bytestring will leave ascii characters untouched and convert unicode correctlyAmicable
Actually, using the Python 3 version is the only way to be sure the answer is correct, especially in Python 2: Python 3: hashlib.md5("\u00f1".encode("utf-8")).hexdigest() -> '94e9342ecbb1458b6043ecd3bfcbc192' Python 2: hashlib.md5("\u00f1").hexdigest() -> '8d70428c396cafbf791f79f1c5172cd7' Python 2: hashlib.md5(u"\u00f1".encode("utf-8")).hexdigest() -> '94e9342ecbb1458b6043ecd3bfcbc192'Amicable
For Python 3, is there a reason to use .encode('utf-8') instead of .encode() without parameter? It works great without the 'utf-8' string like in this answer: #4955102 By the way, I read Python 3 is easier than Python 2 but here it is not the case since the .encode() extra step is not needed at all in Python 2...Schumann
P
297

For Python 2.x, use python's hashlib

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

Output: a02506b31c1cd46c2e0b6380fb94eb3d

Polygnotus answered 14/3, 2011 at 10:43 Comment(8)
Don't try to use hashlib.md5().update('your string').hexdigest(), it won't work since update() returns None. If you want a one line solution, use Mark Longair's answer.Prizefight
@ChristopherManning m.hexdigest() returns a 32 character long digest. How to get a 16 character long digest?Betts
@Darwesh it's 32 characters because it's the hex representation, do a m.digest_size on top of this code, internal digest is already 16 bytes.Genovese
@Darwesh you can simply slice the string m.hexdigest()[:16]Geosynclinal
@Darwesh According to RFC 1321, the md5 is always 16 bytes. If you just want a 16 character long digest, you can do a slice as Baris Demiray said.Commander
work like charm, i used it to generate my hash password on dbAnkylosis
@Ankylosis It's not recmmeded to use MD5 for password hashes nowadays. THey can be bruteforced very fast, so it offers little protection.Polygnotus
The correct way in Python 3.7 is m.update( "text".encode('utf-8') ) otherwise an error will come out!Tester
E
21

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
Emelineemelita answered 13/11, 2019 at 18:45 Comment(2)
What about in front of a string variable? This syntax doesn't work for b'string_variable.Professional
@flwJlxSzApHEZII, try string_variable.encode('utf-8')Bulgar
P
13

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.

Playful answered 14/3, 2011 at 10:41 Comment(2)
hexdigest() returns a 32 character long digest. How to get a 16 character long digest?Betts
@Darwesh: Well yes, MD5 is 128 bits, which is 32 characters in hex. If you want a smaller digest, you'll need a 64-bit digest. That will be pretty weak though...Playful
K
12

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().

Kern answered 7/1, 2021 at 13:22 Comment(0)
B
5

This worked for me on windows 10:

import hashlib

print(hashlib.md5("string to encode".encode('utf-8')).hexdigest())
Brough answered 7/3, 2022 at 9:40 Comment(0)
L
4
Try This 
import hashlib
user = input("Enter text here ")
h = hashlib.md5(user.encode())
h2 = h.hexdigest()
print(h2)
Ld answered 10/6, 2018 at 9:0 Comment(0)
G
2

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)
Gog answered 29/6, 2019 at 14:58 Comment(0)
S
2

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'
Stromberg answered 31/10, 2021 at 8:13 Comment(0)
H
0

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)
Hypanthium answered 27/12, 2022 at 6:4 Comment(2)
This might not produce consistent results, depending on the internal implementation / internal data structure of hashlib.md5(). You can also produce a list of numbers with: list(hashlib.md5(b'hi').digest())Bendite
If you need portability, you can set the endian-ness.Hypanthium

© 2022 - 2024 — McMap. All rights reserved.