384

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

Muhammad Raihan Muhaimin
  • 5,559
  • 7
  • 47
  • 68
super9
  • 29,181
  • 39
  • 119
  • 172
  • 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: https://docs.python.org/3/library/hashlib.html – Danny Varod Jan 25 '23 at 12:55

10 Answers10

778

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

akki
  • 2,021
  • 1
  • 24
  • 35
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 1
    hexdigest() returns a 32 character long digest. How to get a 16 character long digest? – Mahammad Adil Azeem Jun 15 '16 at 19:03
  • 3
    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 Longair – Jeff Hu Nov 20 '17 at 05:04
  • 4
    @JeffHu, because `hashlib.md5` expects [a bytes-like-object](https://docs.python.org/3/glossary.html#term-bytes-like-object) – MaxU - stand with Ukraine Jan 25 '18 at 15:48
  • 1
    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 correctly – Charles L. Aug 20 '18 at 16:13
  • 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'` – Charles L. Aug 20 '18 at 16:22
  • 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: https://stackoverflow.com/questions/4954602/replacement-for-md5-module-in-python-3/4954618#4954618 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... – baptx Jan 21 '19 at 11:50
294

For Python 2.x, use python's hashlib

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

Output: a02506b31c1cd46c2e0b6380fb94eb3d

Muhammad Raihan Muhaimin
  • 5,559
  • 7
  • 47
  • 68
Ikke
  • 99,403
  • 23
  • 97
  • 120
  • 92
    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. – Christopher Manning Nov 16 '11 at 18:39
  • @ChristopherManning m.hexdigest() returns a 32 character long digest. How to get a 16 character long digest? – Mahammad Adil Azeem Jun 15 '16 at 19:03
  • @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. – Baris Demiray Sep 20 '16 at 16:59
  • 1
    @Darwesh you can simply slice the string `m.hexdigest()[:16]` – fedterzi Oct 17 '16 at 13:12
  • @Darwesh According to [RFC 1321](https://tools.ietf.org/html/rfc1321.html), the md5 is always 16 bytes. If you just want a 16 character long digest, you can do a slice as Baris Demiray said. – ryan Dec 06 '16 at 10:20
  • work like charm, i used it to generate my hash password on db – yussan Jun 07 '18 at 16:18
  • @yussan It's not recmmeded to use MD5 for password hashes nowadays. THey can be bruteforced very fast, so it offers little protection. – Ikke Jun 08 '18 at 17:29
  • The correct way in Python 3.7 is `m.update( "text".encode('utf-8') )` otherwise an error will come out! – loretoparisi Jul 23 '20 at 15:56
20

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
prosti
  • 42,291
  • 14
  • 186
  • 151
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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • hexdigest() returns a 32 character long digest. How to get a 16 character long digest? – Mahammad Adil Azeem Jun 15 '16 at 19:03
  • 1
    @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... – Jon Skeet Jun 15 '16 at 19:08
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().

z0gSh1u
  • 355
  • 3
  • 6
4
Try This 
import hashlib
user = input("Enter text here ")
h = hashlib.md5(user.encode())
h2 = h.hexdigest()
print(h2)
Ome Mishra
  • 59
  • 3
4

This worked for me on windows 10:

import hashlib

print(hashlib.md5("string to encode".encode('utf-8')).hexdigest())
S.B
  • 13,077
  • 10
  • 22
  • 49
Cyebukayire
  • 795
  • 7
  • 14
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)
Md Jewele Islam
  • 939
  • 8
  • 18
1

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'
Ershan
  • 623
  • 8
  • 9
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)
  • 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())` – moo Dec 27 '22 at 08:01
  • If you need portability, you can set the endian-ness. – Dale Johnson Dec 29 '22 at 20:19