Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将PHP哈希转换为Python_Python_Php - Fatal编程技术网

如何将PHP哈希转换为Python

如何将PHP哈希转换为Python,python,php,Python,Php,我可以使用PHP创建随机盐,效果非常好。下面是我的php代码 $random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE)); 这将生成128个字符,如下所示 647366c3950b5fa89d6c71eaa9933c0e8cdf3e09b903641db41fdfe2591cef92aac46cc7b3b36c664bdbe4544aa24476eecbe19b21317c733e970998563e

我可以使用PHP创建随机盐,效果非常好。下面是我的php代码

$random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE));
这将生成128个字符,如下所示

647366c3950b5fa89d6c71eaa9933c0e8cdf3e09b903641db41fdfe2591cef92aac46cc7b3b36c664bdbe4544aa24476eecbe19b21317c733e970998563e6a50

Python中如何实现这一点?

以下是您在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())
示例输出

aa8434ae552feb1421ec4a4a7273b10d3daebf15eaf0620cc61ce1e5d8bd3439f3ab0b23c7048bfa95071fc20c6244269fbfab68af024b6b3d30e393e31209e1

这将每次生成128个字符的唯一salt

到目前为止您尝试了什么?