Python's cryptographic modules md5, SHA, crypt usages _python

Source: Internet
Author: User
Tags base64 crypt md5 sha1 encryption

The MD5 (message-digest algorithm 5) module is used to compute the information ciphertext (Information Digest) and to obtain a 128-bit ciphertext. The SHA module is similar to MD5, but generates a 160-bit signature. The use method is the same.

The following examples are used with MD5:

Copy Code code as follows:

#/usr/bin/python
#-*-Coding:utf-8-*-
Import Base64
Try
Import Hashlib
hash = HASHLIB.MD5 ()
Except Importerror:
# for Python << 2.5
Import MD5
hash = Md5.new ()
Hash.update (' Spam,spam,and egges ')
Value = Hash.digest ()
Print repr (value) #得到的是二进制的字符串
Print hash.hexdigest () #得到的是一个十六进制的值
Print base64.encodestring (value) #得到base64的值

Copy Code code as follows:

#/usr/bin/python
#-*-Coding:utf-8-*-
# Authentication of client and server-side communication information

Import string
Import Random

Def getchallenge ():
Challenge = Map (lambda i:chr (Random.randint (0,255)), Range (16))
Return String.Join (Challenge, "")

def getresponse (Password,challenge):
Try
Import Hashlib
hash = HASHLIB.MD5 ()
Except Importerror:
# for Python << 2.5
Import MD5
hash = Md5.new ()
Hash.update (password)
Hash.update (Challenge)
Return Hash.digest ()

Print "Client:", "Connect"
Challenge= Getchallenge ()
Print "Server:", repr (Challenge)
Client_response = GetResponse ("Trustno1", challenge)
Print "Client:", repr (Client_response)
Server_response = GetResponse ("Trustno1", challenge)
if Client_response = = Server_response:
Print "Server:", "Login OK"

The Crypt Module (Unix only) implements one-way DES encryption, which is used by UNIX systems to store passwords, and this module is really only useful when checking for such passwords.

The following example shows how to use Crypt.crypt to encrypt a password, combine the password and salt and pass it to the function, where the salt contains two-bit random characters. Now you can discard the original password and save only the encrypted string.

Copy Code code as follows:

#/usr/bin/python
#-*-Coding:utf-8-*-

Import Crypt
Import random,string

def getsalt (chars = string.letters+string.digits):
Return Random.choice (chars) +random.choice (chars)

Salt = GetSalt ()
Print Salt
Print Crypt.crypt (' bananas ', salt)

PS: About encryption technology, the site also provides the following encryption tools for your reference to use:

MD5 Online encryption tool: Http://tools.jb51.net/password/CreateMD5Password

Escape Encryption/decryption tool: http://tools.jb51.net/password/escapepwd

Online SHA1 encryption tool: Http://tools.jb51.net/password/sha1encode

short link (short URL) online generation tool: http://tools.jb51.net/password/dwzcreate

short link (short URL) online Restore tool: Http://tools.jb51.net/password/unshorturl

High Strength password generator: Http://tools.jb51.net/password/CreateStrongPassword

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.