Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.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
Python 使用hashlib';谁的更新方法?_Python_Cryptography_Sha256_Hashlib_Cryptographic Hash Function - Fatal编程技术网

Python 使用hashlib';谁的更新方法?

Python 使用hashlib';谁的更新方法?,python,cryptography,sha256,hashlib,cryptographic-hash-function,Python,Cryptography,Sha256,Hashlib,Cryptographic Hash Function,我已经编写了一个使用hashlib对密码进行散列的方法。 我允许用户通过POST方法发送密码,该方法由Flask接收,然后对密码进行散列,以便可以根据存储的var检查散列是否相同 当第一次发送正确或不正确的密码时,它都非常有效。 但是,如果用户在第一次发布时发送了错误的密码,然后使用正确的密码重试,则会失败。(如果第一次尝试成功,用户继续尝试,也可以认为失败,但我现在不在乎。) hash.update(arg) 使用字符串arg更新哈希对象。重复调用相当于连接所有参数的单个调用:m.updat

我已经编写了一个使用hashlib对密码进行散列的方法。 我允许用户通过POST方法发送密码,该方法由Flask接收,然后对密码进行散列,以便可以根据存储的var检查散列是否相同

当第一次发送正确或不正确的密码时,它都非常有效。 但是,如果用户在第一次发布时发送了错误的密码,然后使用正确的密码重试,则会失败。(如果第一次尝试成功,用户继续尝试,也可以认为失败,但我现在不在乎。)

hash.update(arg) 使用字符串arg更新哈希对象。重复调用相当于连接所有参数的单个调用:m.update(a);m、 update(b)相当于m.update(a+b)

我想知道如何在重复调用时禁用连接。 不管这是不是一个黑客的解决办法

以下是我的代码,以防有用:

h = hashlib.sha256()
VALID_USERNAME = 'admin'
VALID_PASSW_HASH = "210ce034be6d826a451a4261d70494148c5d7101627335ccacf8e00a711bcc5d"

@app.route('/api/queue/auth', methods=['POST'])
def auth():
    username = request.json.get('username')
    password = request.json.get('password')
    if bool(username) is False or bool(password) is False:
        return "\nPlease fill in both fields.\n", 400
    passwordBytes = password.encode(encoding='UTF-8',errors='strict')
    h.update(passwordBytes)
    if h.hexdigest() != VALID_PASSW_HASH or username != VALID_USERNAME:
        return "\nPlease check your username and password, and try again.\n", 401
    r.set('auth', 'true')
    return "Access Granted.\n", 200
补充说明:

  • “r.set”行(位于最后一行之上)只是因为它后来对Redis做了一些事情
  • 我已经检查过,当提供相同的密码时,passwordBytes总是返回相同的编码(这是确定的)
  • 我还检查了h.hexdigest()是否在第一次尝试或另一次尝试时提供相同的密码时返回不同的内容。考虑到这两点,我们可以确定问题出在h.update()中,这可能是因为连接特性

只需将第一行从全局范围移到
auth()函数中即可:

VALID_USERNAME = 'admin'
VALID_PASSW_HASH = "210ce034be6d826a451a4261d70494148c5d7101627335ccacf8e00a711bcc5d"

@app.route('/api/queue/auth', methods=['POST'])
def auth():
    username = request.json.get('username')
    password = request.json.get('password')
    if bool(username) is False or bool(password) is False:
        return "\nPlease fill in both fields.\n", 400
    passwordBytes = password.encode(encoding='UTF-8',errors='strict')
    h = hashlib.sha256()
    h.update(passwordBytes)
    if h.hexdigest() != VALID_PASSW_HASH or username != VALID_USERNAME:
        return "\nPlease check your username and password, and try again.\n", 401
    r.set('auth', 'true')
    return "Access Granted.\n", 200
VALID_USERNAME = 'admin'
VALID_PASSW_HASH = "210ce034be6d826a451a4261d70494148c5d7101627335ccacf8e00a711bcc5d"

def hash_password(password):
    passwordBytes = password.encode(encoding='UTF-8',errors='strict')
    h = hashlib.sha256()
    h.update(passwordBytes)
    return h.hexdigest()


@app.route('/api/queue/auth', methods=['POST'])
def auth():
    username = request.json.get('username')
    password = request.json.get('password')
    if bool(username) is False or bool(password) is False:
        return "\nPlease fill in both fields.\n", 400
    if hash_password(password) != VALID_PASSW_HASH or username != VALID_USERNAME:
        return "\nPlease check your username and password, and try again.\n", 401
    r.set('auth', 'true')
    return "Access Granted.\n", 200
或者更好的方法是,将密码哈希重构为不同的函数:

VALID_USERNAME = 'admin'
VALID_PASSW_HASH = "210ce034be6d826a451a4261d70494148c5d7101627335ccacf8e00a711bcc5d"

@app.route('/api/queue/auth', methods=['POST'])
def auth():
    username = request.json.get('username')
    password = request.json.get('password')
    if bool(username) is False or bool(password) is False:
        return "\nPlease fill in both fields.\n", 400
    passwordBytes = password.encode(encoding='UTF-8',errors='strict')
    h = hashlib.sha256()
    h.update(passwordBytes)
    if h.hexdigest() != VALID_PASSW_HASH or username != VALID_USERNAME:
        return "\nPlease check your username and password, and try again.\n", 401
    r.set('auth', 'true')
    return "Access Granted.\n", 200
VALID_USERNAME = 'admin'
VALID_PASSW_HASH = "210ce034be6d826a451a4261d70494148c5d7101627335ccacf8e00a711bcc5d"

def hash_password(password):
    passwordBytes = password.encode(encoding='UTF-8',errors='strict')
    h = hashlib.sha256()
    h.update(passwordBytes)
    return h.hexdigest()


@app.route('/api/queue/auth', methods=['POST'])
def auth():
    username = request.json.get('username')
    password = request.json.get('password')
    if bool(username) is False or bool(password) is False:
        return "\nPlease fill in both fields.\n", 400
    if hash_password(password) != VALID_PASSW_HASH or username != VALID_USERNAME:
        return "\nPlease check your username and password, and try again.\n", 401
    r.set('auth', 'true')
    return "Access Granted.\n", 200

这个计划是不安全的。请使用正确的密码哈希,而不是消息摘要函数。Werkzueg的密码哈希是安全的。