Flask-Bcrypt Chinese document released !, Flask-bcrypt document

Source: Internet
Author: User
Tags rounds

Flask-Bcrypt Chinese document released !, Flask-bcrypt document

Flask-Bcrypt-flask-bcrypt-docs-zh 0.5 document

Flask-Bcrypt is a Flask extension that provides the bcrypt hash function for your application.

Thank you for your criticism.

Flask-Bcrypt

Flask-Bcrypt is a Flask extension that provides the bcrypt hash function for your applications.

Thanks to the improvement in the performance of computer hardware (such as GPU) in recent years, Hash technology has become very easy to crack. To solve this problem, a positive approach is to use a "optimize" hashing technology. Bcrypt is such a hash tool. Unlike speed-optimized hash algorithms such as MD5 and SHA1, bcrypt is intentionally slowed down.

Password and other sensitive data must be protected, and bcrypt is a recommended choice.

Install

To install the extension, run the following command ::

$ easy_install flask-bcrypt

 

If you have installed pip, you can select the following method ::

$ pip install flask-bcrypt

 

Annotation

You need Python Development Headers as dependencies to install the py-bcrypt package. If you are using Mac OS or Windows, you may have installed it. If you are not using the two but Linux, you need to find the package based on your system. For a Dabian-based release, find the package named python-dev. For a RedHat-based release, find the package named python-devel.

Usage

You want to easily import the encapsulated classes and import the flask application through extensions. We should do this ::

from flask import Flaskfrom flask.ext.bcrypt import Bcryptapp = Flask(__name__)bcrypt = Bcrypt(app)

The two basic hash methods are already included in the bcrypt object. In Python2, you can use them as follows ::

pw_hash = bcrypt.generate_password_hash('hunter2')bcrypt.check_password_hash(pw_hash, 'hunter2') # returns True

In python3, you need to use the decode ('utf-8') method on generate_password_hash (), as shown below ::

pw_hash = bcrypt.generate_password_hash(‘hunter2’).decode(‘utf-8’)
APIclass flask_bcrypt.Bcrypt (app = None)

Bcrypt is a class container used to hash passwords and check the logic. This class may be used to initialize your flask app object. The function of this class is to provide a simple interface to override the built-in password hash function of Werkzeug.

Although these methods are not actually rewritten, APIs are intentionally designed to be very similar, this makes it easy for applications that have used Hash Functions to rewrite to bcrypt with powerful capabilities.

To start using bcrypt, you can wrap your application as follows ::

app = Flask(__name__)bcrypt = Bcrypt(app)

Now, two basic functions are included in the app object through bcrypt. Then, in the context of the application, important data (such as passwords) can be hashed by the following operations ::

password = 'hunter2'pw_hash = bcrypt.generate_password_hash(password)

Once hashed, the value is irreversible. However, in some scenarios where you need to confirm the password and compare the hash values of the two passwords, it is very necessary to set the constant time (Translator's note: big-O (n )) processing completed. This prevents time series attacks (timing attacks ). Bcrypt provides a simple method, as shown below ::

candidate = 'secret'bcrypt.check_password_hash(pw_hash, candidate)

If the two passwords are the same, the check_password_hash method returns True. If they are inconsistent, False is returned.

If you use bcrypt in the form of bcrypt = Bcrypt (app), pay attention to this issue. You actually have rewritten the bcrypt module. Although you may not need to operate a module that is out of the extended range, you should be aware that the module has been overwritten. Or you can consider using a different name to avoid name conflicts, such as flask_bcrypt = Bcrypt (app ).

The BCRYPT_LOG_ROUNDS option can be configured in the flask application. This value determines the complexity of encryption. The default value is 12. (For details, see bcrypt .)

The BCRYPT_HASH_PREFIX option can be configured in the flask application. This value can be used to set the hash version. The default value is 2b. (For details, see bcrypt .)

In the bcrypt algorithm, the default password length is 72 bits. If the password exceeds 72 bits, the password is ignored. If a given password is hashed out, common solutions will use the hexdigest method in the hexdigest technology (such as sha256) to avoid the problem of no bit, and then use bcrypt to hash the results. If the value of BCRYPT_HANDLE_LONG_PASSWORDS is set to True, the preceding solution cannot be used. Warning do not set this option to True in a project that has already used flask-bcrypt; otherwise, you will be unable to use the password verification function. Warning if this option has been set to True in an existing project, but you change it to False again, the password check function will not be available.

Parameters:

  • App-Flask application object. The default value is null.
Check_password_hash (pw_hash, password)

Check whether the hash value matches the password. The password is hashed and then compared with the hash value within the constant time. This method returns True or False.

The usage example of check_password_hash is as follows ::

pw_hash = bcrypt.generate_password_hash('secret', 10)bcrypt.check_password_hash(pw_hash, 'secret') # returns True

Parameters:

  • Pw_hash-Hash value used for comparison.
  • Password-Password used for comparison.
Generate_password_hash (password, rounds = None, prefix = None)

Use bcrypt to generate a hash value for a password. You can set the parameter log_rounds of bcrypt. gensalt () to determine the complexity of the salt. The default value is 12. If you specify the prefix value, the prefix of the bcrypt. gensalt () parameter can be cold, and the version of the algorithm that generates the hash value can be determined.

The usage example of generate_password_hash is as follows ::

pw_hash = bcrypt.generate_password_hash('secret', 10)

Parameters:

  • Password-Hash password.
  • Rounds-Set the salt complexity.
  • Prefix-Set the algorithm version.
Init_app (app)

Initialize the application with extensions.

Parameters:

  • App-Flask application object.
Flask_bcrypt.generate_password_hash (password, rounds = None)

This function packages the eponyous method of Bcrypt for your convenience. It is expected to be used as a help function when the app has been configured. In other cases, the app object is not used for this convenient usage.

For this function, it is feasible to simply import it from the module and use it as a similar method. Here is a simple example ::

from flask.ext.bcrypt import generate_password_hashpw_hash = generate_password_hash('hunter2', 10)

Parameters:

  • Password-Hash password.
  • Ronuds-Salt complexity.
Flask_bcrypt.check_password_hash (pw_hash, password)

This function packages the eponyous method of Bcrypt for your convenience. It is expected to be used as a help function when the app has been configured. In other cases, the app object is not used for this convenient usage.

For this function, it is feasible to simply import it from the module and use it as a similar method. Here is a simple example ::

from flask.ext.bcrypt import check_password_hashcheck_password_hash(pw_hash, 'hunter2') # returns True

Parameters:

  • Pw_hash-Hash value used for comparison.
  • Password-Password used for comparison.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.