Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert simple php code to python

Tags:

python

php

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.

like image 663
Hitul Mistry Avatar asked Apr 19 '26 17:04

Hitul Mistry


1 Answers

import hashlib
secret, path, expire = 'segredo', '/p/files/top_secret.pdf', 1096891200
md5 = hashlib.md5(secret + path + str(expire)).digest().encode('base64').strip('\n=')
like image 86
defuz Avatar answered Apr 22 '26 07:04

defuz