简体   繁体   English

不确定 'if float(message) >= time.time():' 在代码中的作用

[英]not sure what the 'if float(message) >= time.time():' does in the code

import cryptocode
import time
import sys
#Function to verify that the key is valid:
def check_valid(key):
    message = cryptocode.decrypt(key, 'cryptocode is amazing')
    if message == False:
        #The key is incorrect!
        return False
    if float(message) >= time.time():
        return True
    else:
        #The key has expired!
        return False
userKeyInput = input("Please enter your product key.")
keyChecked = check_valid(userKeyInput)
if keyChecked == True:
    print("You are good to go!")
if keyChecked == False:
    print("You have either entered an invalid key or your time has expired. Sorry!")
    sys.exit()
in this code we're creating a simple "trial product key".在这段代码中,我们创建了一个简单的“试用产品密钥”。 we're letting the user use the product for 2 hours.我们让用户使用该产品 2 小时。 The password we will be using is cryptocode is amazing.我们将使用的密码是密码密码是惊人的。

but I'm not able to grasp the significance of this statement in this program: if float(message) >= time.time(): , and what it is actually doing here但我无法理解此程序中此语句的意义: if float(message) >= time.time(): ,以及它在这里实际执行的操作

It would be appear to be the case that if successfully decrypted (ie if 'cryptocode is amazing' was the correct password), then message (the content of key once decrypted) contains the time of expiration in float format.如果成功解密(即如果'cryptocode is amazing'是正确的密码),那么message (解密后的key内容)似乎会包含浮点格式的到期时间。 Hence, the line you're asking about converts it into float and compares it with the current time to check if it had already expired.因此,您要询问的行将其转换为浮点数并将其与当前时间进行比较以检查它是否已经过期。 (If the current time > expiration time, then the expiration time is in the past: the key had, indeed, already expired.) (如果当前时间 > 过期时间,那么过期时间是过去的:密钥确实已经过期了。)

In other words: there's an implicit assumption here that the key given to the user was the expiration time, encrypted.换句话说:这里有一个隐含的假设,即提供给用户的密钥是加密的到期时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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