1

I'm trying to get the md5 in both php and python, but I'm not sure why the results are different, I have read in other questions about hashing strings but not files and I've also tried the echo -n but I get a syntax error.

Php:

<?php
  echo 'MD5 file hash : ' . md5_file('https://cdn4.buysellads.net/uu/1/8026/1533152372-laptop_purple_graph.png');
?>

MD5 file hash : 5e81ca561d2c1e96b5e7a2e57244c8e5

python:

import hashlib

m=hashlib.md5('https://cdn4.buysellads.net/uu/1/8026/1533152372-laptop_purple_graph.png')
print('The MD5 checksum is',m.hexdigest())

MD5 file hash : 52e8e2e35519e8f6da474c5e1dc6d258

gaby
  • 117
  • 1
  • 11

1 Answers1

0

In Python snippet you are hashing the https://cdn4.buysellads.net/uu/1/8026/1533152372-laptop_purple_graph.png string, which I guess is different from the contents.

You need to fetch url contents first and pass it to hashlib.md5:

import urllib.request
import hashlib

contents = urllib.request.urlopen("https://cdn4.buysellads.net/uu/1/8026/1533152372-laptop_purple_graph.png").read()

m = hashlib.md5(content)
print('The MD5 checksum is',m.hexdigest())
Nebril
  • 3,153
  • 1
  • 33
  • 50
  • I can't believe I didn't notice it thank you! this seems to work well in python 3, but what about python 2.7? – gaby Aug 21 '18 at 20:31
  • @gaby check out this answer: https://stackoverflow.com/questions/645312/what-is-the-quickest-way-to-http-get-in-python – Nebril Aug 22 '18 at 10:43