简体   繁体   中英

how to convert PHP hash into Python

I can create random salt using PHP and it works great. Below is my php code

$random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE));

this generate 128 character like below

647366c3950b5fa89d6c71eaa9933c0e8cdf3e09b903641db41fdfe2591cef92aac46cc7b3b36c664bdbe4544aa24476eecbe19b21317c733e970998563e6a50

How can i achieve that in Python?

Here is the solution what you want in python

import uuid
import hashlib

def random_salt():
    s =  uuid.uuid4().hex
    s = hashlib.sha512(str(s).encode('utf-8')).hexdigest()
    return s

print(random_salt())

example output

aa8434ae552feb1421ec4a4a7273b10d3daebf15eaf0620cc61ce1e5d8bd3439f3ab0b23c7048bfa95071fc20c6244269fbfab68af024b6b3d30e393e31209e1

this will generate unique salt in 128 character every time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM