'how to use python hashlib - by Michael'
,並附上這篇文章的摘要是'2d73d4f15c0db7f5ecb321b6a65e5d6d'
。若是有人篡改了你的文章,並發表爲'how to use python hashlib - by Bob'
,你能夠一會兒指出Bob篡改了你的文章,由於根據'how to use python hashlib - by Bob'
計算出的摘要不一樣於原始文章的摘要。f()
對任意長度的數據data
計算出固定長度的摘要digest
,目的是爲了發現原始數據是否被人篡改過。f(data)
很容易,但經過digest
反推data
卻很是困難。並且,對原始數據作一個bit的修改,都會致使計算出的摘要徹底不一樣。咱們以常見的摘要算法MD5爲例,計算出一個字符串的MD5值:python
import hashlib md5 = hashlib.md5() md5.update('how to use md5 in python hashlib?'.encode('utf-8')) print(md5.hexdigest()) ## 計算結果以下 d26a53750bc40b38b65a520292f69306
update()
,最後計算的結果是同樣的:
import hashlib md5 = hashlib.md5() md5.update('how to use md5 in '.encode('utf-8')) md5.update('python hashlib?'.encode('utf-8')) print(md5.hexdigest())
import hashlib sha1 = hashlib.sha1() sha1.update('how to use sha1 in '.encode('utf-8')) sha1.update('python hashlib?'.encode('utf-8')) print(sha1.hexdigest())
'how to learn hashlib in python - by Bob'
,而且這篇文章的摘要剛好和你的文章徹底一致,這種狀況也並不是不可能出現,可是很是很是困難。
def calc_md5(password): pass
""" michael:123456 bob:abc999 alice:alice2008 """ import hashlib db = { 'michael': 'e10adc3949ba59abbe56e057f20f883e', 'bob': '878ef96e86145580c38c87f0410ad153', 'alice': '99b1c2188db85afee403b1536010c2c9' } def login(user, password): md5 = hashlib.md5() md5.update(password.encode('utf-8')) if db[user] == md5.hexdigest(): return True else: return False print(login('michael', '123456')) print(login('bob', 'abc999')) print(login('alice', 'alice2008')) print(login('michael', '1234567')) print(login('bob', '123456')) print(login('alice', 'Alice2008'))
123456
,888888
,password
這些簡單的口令,因而,黑客能夠事先計算出這些經常使用口令的MD5值,獲得一個反推表:
'e10adc3949ba59abbe56e057f20f883e': '123456' '21218cca77804d2ba1922c33e0151105': '888888' '5f4dcc3b5aa765d61d8327deb882cf99': 'password'
def calc_md5(password): return get_md5(password + 'the-Salt')
123456
,在數據庫中,將存儲兩條相同的MD5值,這說明這兩個用戶的口令是同樣的。有沒有辦法讓使用相同口令的用戶存儲不一樣的MD5呢?
import hashlib, random def get_md5(s): return hashlib.md5(s.encode('utf-8')).hexdigest() class User(object): def __init__(self, username, password): self.username = username self.salt = ''.join([chr(random.randint(48, 122)) for i in range(20)]) self.password = get_md5(password + self.salt) db = { 'michael': User('michael', '123456'), 'bob': User('bob', 'abc999'), 'alice': User('alice', 'alice2008') } def login(username,password): ss=get_md5(password +db[username].salt) if db[username].password==str(ss): return True else: return False print(login('michael', '123456')) print(login('bob', 'abc999')) print(login('alice', 'alice2008')) print(login('michael', '1234567')) print(login('bob', '123456')) print(login('alice', 'Alice2008'))
import hashlib md5 = hashlib.md5() md5.update('how to use md5 in python hashlib?'.encode('utf-8')) print(md5.hexdigest()) sha1 = hashlib.sha1() sha1.update('how to use sha1 in '.encode('utf-8')) sha1.update('python hashlib?'.encode('utf-8')) print(sha1.hexdigest())
>>> from io import StringIO >>> f = StringIO() >>> f.write('hello') 5 >>> f.write(' ') 1 >>> f.write('world!') 6 >>> print(f.getvalue()) hello world!
getvalue()
方法用於得到寫入後的str。>>> from io import StringIO >>> f = StringIO('Hello!\nHi!\nGoodbye!') >>> while True: ... s = f.readline() ... if s == '': ... break ... print(s.strip()) ... Hello! Hi! Goodbye!
>>> from io import BytesIO >>> f = BytesIO() >>> f.write('中文'.encode('utf-8')) 6 >>> print(f.getvalue()) b'\xe4\xb8\xad\xe6\x96\x87'
>>> from io import BytesIO >>> f = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87') >>> f.read() b'\xe4\xb8\xad\xe6\x96\x87'
## do_stringio.py from io import StringIO # write to StringIO: f = StringIO() f.write('hello') f.write(' ') f.write('world!') print(f.getvalue()) # read from StringIO: f = StringIO('水面細風生,\n菱歌慢慢聲。\n客亭臨小市,\n燈火夜妝明。') while True: s = f.readline() if s == '': break print(s.strip()) ## do_bytesio.py from io import BytesIO # write to BytesIO: f = BytesIO() f.write(b'hello') f.write(b' ') f.write(b'world!') print(f.getvalue()) # read from BytesIO: data = '人閒桂花落,夜靜春山空。月出驚山鳥,時鳴春澗中。'.encode('utf-8') f = BytesIO(data) print(f.read())
import json # Python 字典類型轉換爲 JSON 對象 data = { 'no' : 1, 'name' : 'Runoob', 'url' : 'http://www.runoob.com' } json_str = json.dumps(data) print ("Python 原始數據:", repr(data)) print ("JSON 對象:", json_str) ## 執行以上代碼輸出結果爲: Python 原始數據: {'url': 'http://www.runoob.com', 'no': 1, 'name': 'Runoob'} JSON 對象: {"url": "http://www.runoob.com", "no": 1, "name": "Runoob"}
import json # Python 字典類型轉換爲 JSON 對象 data1 = { 'no' : 1, 'name' : 'Runoob', 'url' : 'http://www.runoob.com' } json_str = json.dumps(data1) print ("Python 原始數據:", repr(data1)) print ("JSON 對象:", json_str) # 將 JSON 對象轉換爲 Python 字典 data2 = json.loads(json_str) print ("data2['name']: ", data2['name']) print ("data2['url']: ", data2['url']) ## 執行以上代碼輸出結果爲: Python 原始數據: {'name': 'Runoob', 'no': 1, 'url': 'http://www.runoob.com'} JSON 對象: {"name": "Runoob", "no": 1, "url": "http://www.runoob.com"} data2['name']: Runoob data2['url']: http://www.runoob.com
# 寫入 JSON 數據 with open('data.json', 'w') as f: json.dump(data, f) # 讀取數據 with open('data.json', 'r') as f: data = json.load(f)
import json test = '[{"a": 1, "aa": 11, "aaa": 111}, {"b": 2, "bb": 22, "bbb": 222}, {"c":3}]' test2 = '''{'aaa': 111, 'bbb': 222}''' print(type(test)) # json.dumps() newTest = json.loads(test) print(type(newTest)) print(newTest[0]["a"]) xxx = json.dumps(newTest) print(type(xxx)) yyy = str(newTest) print(type(yyy)) print(type(json.loads(xxx))) # print(type(json.loads(yyy))) # print(test2) # print(type(test2)) # result = json.loads(test2) # print(result)
import json import requests url = "http://qwd.jd.com/fcgi-bin/qwd_activity_list?g_tk=1231472791&env=3" session = requests.session() r = session.get(url) result = json.loads(r.text) print(result["errCode"]) print(result["msg"])
'''python2 纔有一下的狀況''' import json a = dict(hello="你好") print(a) print(a["hello"]) print(str(a)) print(json.dumps(a, ensure_ascii=False))
import codecs import json test = {"a": 1, "b": 2} with codecs.open("1.txt", "w") as f: json.dump(test, f) with codecs.open("1.txt", "r") as f: aa = json.load(f) print(aa) print(type(aa))