Python-Flask框架(三),如何保護用戶密碼? 淺談網絡安全

我們爲什麼要保護用戶密碼

作爲網站開發人員,保護用戶的密碼不僅僅是我們的責任更是我們的義務,我們應該有一個正義的態度。這樣子才能獲得更好的口碑。簡單的說就是,網絡環境下每個人的隱私十分重要!!!

不對用戶密碼進行加密會有什麼危險

如果我們不對用戶密碼加密,且以明文的方式存儲到數據庫中,假若黑客獲取到我們的數據庫,那所有的用戶密碼就全部都會被暴露出來,所以我們需要對用戶的密碼加密(哪怕數據庫被脫庫了,黑客得到的也只是一堆加密的密文)。只要黑客不知道我們的加密算法,那破解難度就十分大了,簡而言之用戶的密碼就不會被竊取。
so 這就需要一種單向加密的算法,使得加密後的密文,無法反推回明文。

那該如何防範

利用加密算法將用戶的密碼進行加密,防止黑客得到我們數據庫的時候直接暴露用戶密碼。
其實大多數互聯網公司都沒有對用戶的密碼進行加密存儲(2012年附近某DN網站脫庫事件)
存儲通過密碼生成的hash值。每個密碼都對應一個獨一無二的hash值,從而避免明文存儲密碼。
加密算法:
SHA-1,MD5,BASE64。。。
其中 MD5,SHA-1都屬於哈希加密
但是簡單的計算hash值,並沒有達到我們的要求,拿md5講,www.cmd5.com,就可以解決大部分的解密工作,我們則需要加鹽(salt)計算hash值。計算加鹽hash值

在Flask框架中該如何存儲用戶密碼

python中的PassLib,bcrybt,hashlib等
而Flask的主要依賴Werkzeug也提供了這一功能。Werkzeug在security模塊中提供了一個generate_password_hash()函數用於計算給定密碼生成hash值,其中method 用於指定計算hash值的方法,salt_length參數用來指定salt的長度。


def generate_password_hash(password, method="pbkdf2:sha256", salt_length=8):
    """Hash a password with the given method and salt with a string of
    the given length. The format of the string returned includes the method
    that was used so that :func:`check_password_hash` can check the hash.

    The format for the hashed string looks like this::

        method$salt$hash

    This method can **not** generate unsalted passwords but it is possible
    to set param method='plain' in order to enforce plaintext passwords.
    If a salt is used, hmac is used internally to salt the password.

    If PBKDF2 is wanted it can be enabled by setting the method to
    ``pbkdf2:method:iterations`` where iterations is optional::

        pbkdf2:sha256:80000$salt$hash
        pbkdf2:sha256$salt$hash

    :param password: the password to hash.
    :param method: the hash method to use (one that hashlib supports). Can
                   optionally be in the format ``pbkdf2:<method>[:iterations]``
                   to enable PBKDF2.
    :param salt_length: the length of the salt in letters.
    """
    salt = gen_salt(salt_length) if method != "plain" else ""
    h, actual_method = _hash_internal(method, salt, password)
    return "%s$%s$%s" % (actual_method, salt, h)

security模塊中的check_password_hash(pwhash,password)函數用於接受hash值(pwhash)和密碼(password)作爲參數,用於檢查密碼hash值與密碼是否對應

def check_password_hash(pwhash, password):
    """check a password against a given salted and hashed password value.
    In order to support unsalted legacy passwords this method supports
    plain text passwords, md5 and sha1 hashes (both salted and unsalted).

    Returns `True` if the password matched, `False` otherwise.

    :param pwhash: a hashed string like returned by
                   :func:`generate_password_hash`.
    :param password: the plaintext password to compare against the hash.
    """
    if pwhash.count("$") < 2:
        return False
    method, salt, hashval = pwhash.split("$", 2)
    return safe_str_cmp(_hash_internal(method, salt, password)[0], hashval)
使用式例

在這裏插入圖片描述
網絡安全是一個永遠都不會低迷的話題。今天一個web服務,是沒有絕對安全存在的。作爲開發人員,我們不能只會敲代碼,更需要具備一定水平的網絡安全知識,從而使自己的web服務,擁有一個相對安全的處境。所以 “一個好的程序員他也應該是一個黑客”

如有不足歡迎發表問題和建議。如果對你有所幫助也歡迎各位老爺們點贊👍和評論

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章