同一个密钥生成器在PHP和Python中的两个实现有什么不同?

2024-09-28 22:21:09 发布

您现在位置:Python中文网/ 问答频道 /正文

在PHP中实现auth key generator有什么不同:

<?php
$password = "834ff7b651a6cb1b2f39c70bf43d3e78";
$timestamp = round(microtime(true) * 1000);
$hash = md5($password.$timestamp);
echo "\n Timestamp: ".$timestamp;
echo "\n Hash: ".$hash."\n";
?>

在python中:

import hashlib
import time
import ipdb
import math
def microtime(get_as_float = False) :
    if get_as_float:
        return time.time()
    else:
        return '%f %d' % math.modf(time.time())

password = "834ff7b651a6cb1b2f39c70bf43d3e78"
timestamp = round(microtime(get_as_float=True)*1000)
m = hashlib.md5()
m.update(password + str(timestamp))
hash = m.hexdigest()
print(str(int(timestamp)))
print(hash)

如果时间戳生成在PHP中的工作方式不同于在Python中的实现?你知道吗

因为我在对服务进行身份验证时检查了它,该服务将这个时间戳与从密码和时间戳连接在一起计算的哈希成对使用,如果使用PHP,我可以传递,但是如果使用Python,我不能传递。你知道吗

谢谢!你知道吗


Tags: importechogettimeas时间passwordhash
1条回答
网友
1楼 · 发布于 2024-09-28 22:21:09

如果您使用的是python2,请注意round()返回一个浮点值。当您str()它时,它的格式将类似于浮点:

>>> str(round(time.time() * 1000))
'1.48965165075e+12'

(当值≥1011(1973年)时,Python将开始用科学记数法打印数字,但即使是<;1011,最后也会有一个额外的.0。PHP将在≥1014时以科学记数法打印(5138年)

请考虑显式格式:

m.update(password)
m.update('%.0f' % timestamp)

既然你写了print(str(int(timestamp))),那可能只是一个疏忽?你也可以在这里使用str(int(timestamp))

m.update(password)
m.update(str(int(timestamp)))

但在python2中,如果timestamp≥262(年份292278994),则在末尾将有一个额外的L。你知道吗

相关问题 更多 >