zoukankan      html  css  js  c++  java
  • python的加密模块(md5,sha,crypt)学习

    md5(Message-Digest Algorithm 5) 模块用于计算信息密文(信息摘要),得出一个128位的密文。sha模块跟md5相似,但生成的是160位的签名。使用方法是相同的。

    如下实例是使用md5的:

    # /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的值
    # /usr/bin/python
    # -*- coding:utf-8 -*-
    # 客户端与服务器端通信的信息的验证
    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 包含两位随机字符.现在你可以扔掉原密码而只保存加密后的字符串了.

    # /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)
  • 相关阅读:
    c#个人记录常用方法(更新中)
    Newtonsoft.Json.dll解析json的dll文件使用
    组织http请求
    ado.net中的几个对象
    jquery-easyui使用
    aspx与mvc页面验证码
    aspx页面状态管理(查询字符串Request与Application)
    aspx页面状态管理Cookie和ViewState
    在网页中插入qq连接
    ASP.NET中上传图片检测其是否为真实的图片 防范病毒上传至服务器
  • 原文地址:https://www.cnblogs.com/mingaixin/p/2919313.html
Copyright © 2011-2022 走看看