Python cryptographic modules md5, SHA, crypt use instances

Source: Internet
Author: User
Tags crypt sha1 encryption
The MD5 (message-digest algorithm 5) module is used to calculate information ciphertext (information Digest) and to derive a 128-bit cipher. 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 the 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 the Code code as follows:


#/usr/bin/python
#-*-Coding:utf-8-*-
# Validation of information on client-to-server communication

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 for checking 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 throw away the original password and save only the encrypted string.
Copy the 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, this site also provides the following encryption tools for everyone to refer to the 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-chain (short URL) online generation tool: http://tools.jb51.net/password/dwzcreate

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

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

  • 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.