398

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

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.html Commented Jan 25, 2023 at 12:55

10 Answers 10

805

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

Sign up to request clarification or add additional context in comments.

6 Comments

hexdigest() returns a 32 character long digest. How to get a 16 character long digest?
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
@JeffHu, because hashlib.md5 expects a bytes-like-object
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
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'
|
297

For Python 2.x, use python's hashlib

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

Output: a02506b31c1cd46c2e0b6380fb94eb3d

8 Comments

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.
@ChristopherManning m.hexdigest() returns a 32 character long digest. How to get a 16 character long digest?
@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.
@Darwesh you can simply slice the string m.hexdigest()[:16]
@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.
|
22

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

2 Comments

What about in front of a string variable? This syntax doesn't work for b'string_variable.
@flwJlxSzApHEZII, try string_variable.encode('utf-8')
14

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.

2 Comments

hexdigest() returns a 32 character long digest. How to get a 16 character long digest?
@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...
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().

Comments

6

This worked for me on windows 10:

import hashlib

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

Comments

4
Try This 
import hashlib
user = input("Enter text here ")
h = hashlib.md5(user.encode())
h2 = h.hexdigest()
print(h2)

Comments

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)

Comments

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'

Comments

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)

2 Comments

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())
If you need portability, you can set the endian-ness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.