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