The use of the Django self-band Encryption module

Source: Internet
Author: User
Tags auth crypt md5 md5 encryption sha1
However, considering that Django has a user authentication module, it is proven that it has a Cross-platform encryption module. So read the document and find this passage on the https://docs.djangoproject.com/en/1.6/topics/auth/passwords/page:
The code is as follows Copy Code
Manually managing a user ' s password
The django.contrib.auth.hashers module provides a set of functions to create and validate hashed password. can use them independently from the User model.
Check_password ( password, encoded)
If you ' d like to manually authenticate a user by comparing a plain-text password to the hashed password in the database, U Se the convenience function Check_password (). It takes two arguments:the plain-text password to check, and the full value of a user's password field in the Database to check against, and returns True if they match, False otherwise.
Changed in Django 1.6:
In Django 1.4 and 1.5, a blank string is unintentionally considered to is a unusable password, resulting in Returning False for such a password.
Make_password ( password[, salt, hashers])
Creates a hashed password in the format used by this application. It takes one mandatory argument:the password in Plain-text.  Optionally, you can provide a salt and a hashing algorithm to use, if you don ' t want to use the defaults (a password_hashers setting). Currently supported algorithms are: ' pbkdf2_sha256', ' pbkdf2_sha1', ' bcrypt_sha256' (U Sing Bcrypt with Django), ' Bcrypt ', ' SHA1 ', ' MD5', ' unsalted_md5 ' (A for BA Ckward compatibility) and ' crypt ' if you have the Crypt library installed. If The password argument is None, a unusable password are returned (a one that would be never accepted by check_password ()).
Two APIs are given, one to create a password, and a validation password to meet the needs. So hurry and try:

First, introduce modules:

The code is as follows Copy Code

>>> from django.contrib.auth.hashers import Make_password, Check_password

Generate Password:

>>> Make_password ("Www.111cn.net", None, ' pbkdf2_sha256 ')
U ' pbkdf2_sha256$12000$h6hrzd4ddikg$rxbgbtifwadyw+j9o7114vxkvysbvp+lz7osyxkoic0= '

This allows you to use the Django self-contained module to generate a set of passwords, which also features a different password each time it is generated:

  code is as follows copy code

 
>>> Make_password ("Www.111cn.net", None, ' pbkdf2_sha256 ')
U ' pbkdf2_sha256$12000$h6hrzd4ddikg$ rxbgbtifwadyw+j9o7114vxkvysbvp+lz7osyxkoic0= '
 
>>> Make_password ("Www.111cn.net", None, ' pbkdf2_sha256 ')
U ' pbkdf2_sha256$12000$9l09rjd9mbqj$0tjvxbzfn6wwd/qi3weldrrwou7inb7im3ub/np2ppg= '
 
>>> Make_password ("Www.111cn.net", none, ' pbkdf2_sha256 ') = = Make_password ("Www.111cn.net", none,
' pbkdf2_sha256 ')
False

Since each generated ciphertext is not the same, how to verify the user submitted to the plaintext and ciphertext match it? This is done by Check_password, Check_password is very simple to use, just tell it plaintext and ciphertext it will return false or True to verify the results

The code is as follows Copy Code


>>> Text = "Www.111cn.net"
>>> passwd = Make_password (text, None, ' pbkdf2_sha256 ')
>>> Print passwd
pbkdf2_sha256$12000$xzmlhcnvqbb8$i1xdnjipb/crrgrx2x7ym74rnfprcup5pbu6sn+v3j0=
>>> print Check_password (text, passwd)
True

If you don't want to generate different ciphertext each time, you can give the second function of Make_password a fixed string, such as:

The code is as follows Copy Code

>>> Make_password (text, "A", ' pbkdf2_sha256 ')
U ' pbkdf2_sha256$12000$a$5hkipczrzgstkuba5uzzmruawdp2qe6oemhdasvzv4q= '
>>> Make_password (text, "A", ' pbkdf2_sha256 ')
U ' pbkdf2_sha256$12000$a$5hkipczrzgstkuba5uzzmruawdp2qe6oemhdasvzv4q= '

As long as any string can be, and can be multiple. But cannot be empty, for example:

The code is as follows Copy Code


>>> Make_password (Text, "", ' pbkdf2_sha256 ')
U ' pbkdf2_sha256$12000$kbcg81bwmavd$ajngftogfhogoglste2goem3ifkzz1hydsufeqnzhxu= '

>>> Make_password (Text, "", ' pbkdf2_sha256 ')
U ' pbkdf2_sha256$12000$fnv3yu4kgylr$1fi8mxardht6hj/er72ycylgtakw7ymwtj+wv4vhygy= '

A string that is empty is equivalent to:
1

The code is as follows Copy Code

Make_password (text, None, ' pbkdf2_sha256 ')

As for Make_password The third parameter is a way to generate ciphertext, which is roughly the same as the documentation given:

The code is as follows Copy Code

pbkdf2_sha256
Pbkdf2_sha1
bcrypt_sha256
Bcrypt
Sha1
Unsalted_md5
Crypt

The above example I used the first encryption method Pbkdf2_sha256,crypt and Bcrypt all need separate installation module, UNSALTED_MD5 is a common MD5 encryption, if the cryptographic hashing algorithm is not very understanding, So just use Django's latest hash algorithm, pbkdf2_sha256.

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.