I want to convert my php code to python code. Is it possible
$secret = 'segredo'; // To make the hash more difficult to reproduce.
$path = '/p/files/top_secret.pdf'; // This is the file to send to the user.
$expire = 1096891200; // At which point in time the file should expire. time() + x; would be the usual usage.
$md5 = base64_encode(md5($secret . $path . $expire, true)); // Using binary hashing.`$md5 = strtr($md5, '+/', '-_'); // + and / are considered special characters in URLs, see the wikipedia page linked in references.
$md5 = str_replace('=', '', $md5); // When used in query parameters the base64 padding character is considered special.
I want to convert above php code to python. Is there exist some tool for conversion ?
This code is simple unique url generator for nginx HttpSecureLinkModule.
j0k
22.8k28 gold badges81 silver badges90 bronze badges
asked Oct 8, 2012 at 13:56
Hitul Mistry
2,3055 gold badges22 silver badges30 bronze badges
-
1use php2python.com :)defuz– defuz2012年10月08日 14:00:19 +00:00Commented Oct 8, 2012 at 14:00
1 Answer 1
import hashlib
secret, path, expire = 'segredo', '/p/files/top_secret.pdf', 1096891200
md5 = hashlib.md5(secret + path + str(expire)).digest().encode('base64').strip('\n=')
answered Oct 8, 2012 at 14:05
defuz
27.7k10 gold badges40 silver badges60 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
defuz
fix: change
.hexdigest to .digestdefault