飞机场场长

本博客主要摘录python相关的知识,欢迎参阅。

导航

PYTHON 加密相关模块

#-*- coding:utf-8 -*-
from hashlib import md5
content = 12
content_str = str(content)
ciphertext = md5(content_str).hexdigest() #加密
print ciphertext

#from hashlib import md5
#ciphertext_str=raw_input() #写入要解密的密文,如827ccb0eea8a706c4c34a16891f84e7b
#MD5是不可逆的密码加密,可以说除了暴力破解外无法还原,但同样的输入加密出来的结果是一致的,因此要比较输入是否正确,只要比较一下加密后的结果即可,而Python中可以使用hashlib进行MD5加密,具体方法如下

for i in xrange(100000):
ciphertext_tmp = md5(str(i)).hexdigest()
if ciphertext_tmp == ciphertext:
print 'the password is %d' % i
break

python的base64加密解密及md5加密

import hashlib

a = "a test string"
print hashlib.md5(a).hexdigest()
print hashlib.sha1(a).hexdigest()
print hashlib.sha224(a).hexdigest()
print hashlib.sha256(a).hexdigest()
print hashlib.sha384(a).hexdigest()
print hashlib.sha512(a).hexdigest()

import base64
str='haha'
encoded = base64.b64encode(str)
decoded = base64.b64decode(encoded)

1、hashlib
import hashlib
#创建一个哈希对象
md = hashlib.md5()
#md = hashlib.sha1()
#md = hashlib.sha224()
#md = hashlib.sha25()
#md = hashlib.sha384()
#md = hashlib.sha512()
1.1 hashlib.update(arg)
1.2 hashlib.digest() #返回数字形式的哈希
1.3 hashlib.hexdigest() #返回16进制的哈希
1.4 hashlib.copy()
一般而言,用hashlib.hexdigest()就可以了
2、hmac
2.1 hmac.new(key[, msg[, digestmod]])
2.2 hmac.update(msg)
2.3 hmac.digest()
2.4 hmac.hexdigest()
2.5 hmac.copy()

posted on 2013-03-19 04:33  飞机场场长  阅读(479)  评论(0编辑  收藏  举报