-1
import ftplib
import hashlib
import httplib
import pytz
import datetime

localtime = datetime.datetime.now(pytz.timezone('Asia/Singapore')).isoformat()
cam = "hi5"
lscam = localtime + cam
ftp = ftplib.FTP('localhost','username','password')
ftp.cwd('Server')
m=hashlib.md5()
file = open('Desktop/frame00000.png','rb')
m.update(lscam)
dd=m.hexdigest()
ftp.storbinary('STOR '+dd, file)
file.close()
ftp.quit()

How can i generate the actual MD5 hash for "lscam" as this code above keep giving me "weird" md5 hash code and that is not even the actual hash for "lscam". Anybody can help?

Kevin Kai
  • 139
  • 1
  • 1
  • 12

1 Answers1

1

Your lscam variable is the concatenation of datetime.datetime.now() in isoformat, and cam.

Since now() obviously returns different values every time you run the script, the md5 is different everytime.

However, if you compute the hash twice, with the same lscam value, you'll get the same result (as expected).

Small example :

>>> import datetime
>>> import pytz
>>> import hashlib
>>> now = datetime.datetime.now(pytz.timezone('Asia/Singapore')).isoformat()
>>> later = datetime.datetime.now(pytz.timezone('Asia/Singapore')).isoformat()
>>> m1 = hashlib.md5()
>>> m2 = hashlib.md5()
>>> m1.update(now)
>>> m2.update(later)
>>> m1.digest() == m2.digest()
False
3kt
  • 2,543
  • 1
  • 17
  • 29
  • The problem stays the same, you use `now()` in your string, it's therefore always different. The md5 hash reflects those differences, and is never the same. – 3kt Jun 03 '16 at 03:38
  • @KevinKai added a small example to my answer. Tell me if it remains unclear. – 3kt Jun 03 '16 at 03:45
  • Even if it is different MD5 hash but they does not generate the actual MD5 hash for me.. i verify with this website: http://md5.gromweb.com/ – Kevin Kai Jun 03 '16 at 04:12
  • @KevinKai are you adding the _exact same string_ on your website and in your program? You should `print(lscam) ` to make sure. It is *very* unlikely that a wrong md5 is returned (if not impossible) – 3kt Jun 03 '16 at 05:08