MD5(Message-Digest Algorithm 5) 模塊用于計算信息密文(信息摘要),得出一個128位的密文。sha模塊跟md5相似,但生成的是160位的簽名。使用方法是相同的。
如下實例是使用md5的:
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"
crypt 模塊(只用于 Unix)實現了單向的 DES 加密, Unix 系統使用這個加密算法來儲存密碼, 這個模塊真正也就只在檢查這樣的密碼時有用。
如下實例,展示了如何使用 crypt.crypt 來加密一個密碼, 將密碼和 salt組合起來然后傳遞給函數, 這里的 salt 包含兩位隨機字符.現在你可以扔掉原密碼而只保存加密后的字符串了。
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:關于加密技術,本站還提供了如下加密工具供大家參考使用:
MD5在線加密工具:http://tools.VeVB.COm/password/CreateMD5Password
Escape加密/解密工具:http://tools.VeVB.COm/password/escapepwd
在線SHA1加密工具:http://tools.VeVB.COm/password/sha1encode
短鏈(短網址)在線生成工具:http://tools.VeVB.COm/password/dwzcreate
短鏈(短網址)在線還原工具:http://tools.VeVB.COm/password/unshorturl
高強度密碼生成器:http://tools.VeVB.COm/password/CreateStrongPassword
新聞熱點
疑難解答
圖片精選