3

Code in PHP I need to move to python

[md5 key] = md5( ( [Request Timestamp] % [Registration Window] ) . [salt] );

Example:

<?php
function get_md5_hash($strDatetime, $strAuthWindow, $strSalt) {
return md5((strtotime($strDatetime) % ((int)$strAuthWindow)) .$strSalt);
}
print get_md5_hash("2013-04-29T20:51:29GMT+0300", "604800", "155961");
?>

Python code this far

timestamp = datetime.datetime.now() - timedelta(hours=6)
timestamp_str = '{:%Y-%m-%dT%H:%M:%S}'.format(timestamp)

salt = "454243"
registration_window = "604800"

import hashlib

md5_key = hashlib.md5(((timestamp_str % registration_window) .salt).encode('utf-8')).hexdigest()

I can't get past this error message:

TypeError: not all arguments converted during string formatting

3 Answers 3

3

The issue you're facing basically boils down to timestamp_str and registration_window being strings. In Python, one way to format strings is using the % operator. For example,

def sayGreeting(name):
    return "Hello %s" % name

This operator on strings is different than the operator you want to use which is the modulus operator that computes the remainder of two integers.

The strtotime function in PHP returns an integer which is the Unix Timestamp of the string representation of a date and time. To get the same results in Python, you need to convert your timestamp to a Unix Timestamp integer, as well.

Concatenation of strings in PHP is done using the . operator. However, in Python, this is the + operator. This is why you need to change .salt to + salt because you're concatenating the salt to the Unix Timestamp.

I wrote some code to help you get started.

import datetime
import time
import hashlib

timestamp = datetime.datetime.now() - datetime.timedelta(hours=6)
unix_time = time.mktime(timestamp.timetuple())

salt = "454243"
registration_window = 604800

resulting_timestamp = str(unix_time % registration_window)

md5_digest = hashlib.md5(resulting_timestamp + salt).hexdigest()

Remember to never use MD5 for any application where security is a concern. It is not a secure hash function.

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

2 Comments

I would update that to "never use MD5". If security is not a concern, there are much faster hash functions; if it is, MD5 is not secure.
Ya security is not too important, we just need to submit a POST and they verify incoming POSTs this way. What you wrote above definitely makes sense. However, now I get the "Unicode-objects must be encoded before hashing" error. I then tried to add the ".encode('utf-8')" like it was before but no luck.
2

use this

md5_digest = hashlib.md5((resulting_timestamp + salt).encode('utf-8')).hexdigest()

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
2

So after another ~2 weeks of trying to find a solution I still don't have a working python code that will generate the required md5 hash. However I do have a "hack" solution that did work so I'll post that here already and will update in case I find a python solution.

What I did was run the forumla in PHP and import the solution into my python code.

utils.py

import subprocess

proc = subprocess.Popen("php md5.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()

script_response1 = script_response.decode().split('|')[0]

md5.php

<?php

$strAuthWindow = 604800;
$strSalt = "454243";

function get_md5_hash($strDatetime, $strAuthWindow, $strSalt) {
return md5((strtotime($strDatetime) % ((int)$strAuthWindow))
.$strSalt);
}

print("$strDatetime");
print get_md5_hash("$strDatetime,", "$strAuthWindow", "$strSalt");

?>

Comments

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.