Home Backend Development Python Tutorial Salted hash encryption and verification functions for passwords in the Flask framework

Salted hash encryption and verification functions for passwords in the Flask framework

Mar 03, 2017 pm 02:14 PM

密码加密简介

密码存储的主要形式:

  • 明文存储:肉眼就可以识别,没有任何安全性。

  • 加密存储:通过一定的变换形式,使得密码原文不易被识别。

密码加密的几类方式:

  • 明文转码加密:BASE64, 7BIT等,这种方式只是个障眼法,不是真正的加密。

  • 对称算法加密:DES, RSA等。

  • 签名算法加密:也可以理解为单向哈希加密,比如MD5, SHA1等。加密算法固定,容

  • 易被暴力破解。如果密码相同,得到的哈希值是一样的。

  • 加盐哈希加密:加密时混入一段“随机”字符串(盐值)再进行哈希加密。即使密码相同,如果盐值不同,那么哈希值也是不一样的。现在网站开发中主要是运用这种加密方法。

  • 密码生成函数:generate_password_hash

函数定义:

werkzeug.security.generate_password_hash(password, method='pbkdf2:sha1', salt_length=8)
Copy after login

generate_password_hash是一个密码加盐哈希函数,生成的哈希值可通过
check_password_hash()进行验证。

哈希之后的哈希字符串格式是这样的:

method$salt$hash
Copy after login

参数说明:

  • password: 明文密码

  • method: 哈希的方式(需要是hashlib库支持的),格式为

  • pbpdf2:[:iterations]。参数说明:

  • method:哈希的方式,一般为SHA1,

  • iterations:(可选参数)迭代次数,默认为1000。

  • slat_length: 盐值的长度,默认为8。

密码生成示例:

>>> from werkzeug.security import generate_password_hash
>>> print generate_password_hash('123456')
'pbkdf2:sha1:1000$X97hPa3g$252c0cca000c3674b8ef7a2b8ecd409695aac370'
Copy after login

因为盐值是随机的,所以就算是相同的密码,生成的哈希值也不会是一样的。

密码验证函数:check_password_hash
函数定义:

werkzeug.security.check_password_hash(pwhash, password)
Copy after login

check_password_hash函数用于验证经过generate_password_hash哈希的密码
。若密码匹配,则返回真,否则返回假。

参数:

  • pwhash: generate_password_hash生成的哈希字符串

  • password: 需要验证的明文密码

密码验证示例:

>>> from werkzeug.security import check_password_hash
>>> pwhash = 'pbkdf2:sha1:1000$X97hPa3g$252c0cca000c3674b8ef7a2b8ecd409695aac370'
>>> print check_password_hash(pwhash, '123456')
True
Copy after login

举例说明

from werkzeug.security import generate_password_hash, \
   check_password_hash

class User(object):

  def __init__(self, username, password):
    self.username = username
    self.set_password(password)

  def set_password(self, password):
    self.pw_hash = generate_password_hash(password)

  def check_password(self, password):
    return check_password_hash(self.pw_hash, password)
Copy after login

下面来看看是怎么工作的:

>>> me = User('John Doe', 'default')
>>> me.pw_hash
'sha1$Z9wtkQam$7e6e814998ab3de2b63401a58063c79d92865d79'
>>> me.check_password('default')
True
>>> me.check_password('defaultx')
False
Copy after login

小结
上面就是密码生成和验证的方法,一般来说,默认的加密强度已经足够了,如果需
要更复杂的密码,可以加大盐值长度和迭代次数。

更多Flask框架中密码的加盐哈希加密和验证功能相关文章请关注PHP中文网!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles