99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python中hashlib模塊用法示例
python中hashlib模塊用法示例
2018-06-25
收藏

python中hashlib模塊用法示例

我們以前介紹過一篇Python加密的文章:Python 加密的實例詳解。今天我們看看python中hashlib模塊用法示例,具體如下。
hashlib

hashlib主要提供字符加密功能,將md5和sha模塊整合到了一起,支持md5,sha1, sha224, sha256, sha384, sha512等算法
具體應(yīng)用    
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#pyversion:python3.5
#owner:fuzj
 
import hashlib
# ######## md5 ########
string = "beyongjie"
md5 = hashlib.md5()
md5.update(string.encode('utf-8'))   #注意轉(zhuǎn)碼
res = md5.hexdigest()
print("md5加密結(jié)果:",res)
# ######## sha1 ########
sha1 = hashlib.sha1()
sha1.update(string.encode('utf-8'))
res = sha1.hexdigest()
print("sha1加密結(jié)果:",res)
# ######## sha256 ########
sha256 = hashlib.sha256()
sha256.update(string.encode('utf-8'))
res = sha256.hexdigest()
print("sha256加密結(jié)果:",res)
# ######## sha384 ########
sha384 = hashlib.sha384()
sha384.update(string.encode('utf-8'))
res = sha384.hexdigest()
print("sha384加密結(jié)果:",res)
# ######## sha512 ########
sha512= hashlib.sha512()
sha512.update(string.encode('utf-8'))
res = sha512.hexdigest()
print("sha512加密結(jié)果:",res)

輸出結(jié)果:    
md5加密結(jié)果: 0e725e477851ff4076f774dc312d4748
sha1加密結(jié)果: 458d32be8ea38b66300174970ab0a8c0b734252f
sha256加密結(jié)果: 1e62b55bfd02977943f885f6a0998af7cc9cfb95c8ac4a9f30ecccb7c05ec9f4
sha384加密結(jié)果: e91cdf0d2570de5c96ee84e8a12cddf16508685e7a03b3e811099cfcd54b7f52183e20197cff7c07f312157f0ba4875b
sha512加密結(jié)果: 3f0020a726e9c1cb5d22290c967f3dd1bcecb409a51a8088db520750c876aaec3f17a70d7981cd575ed4b89471f743f3f24a146a39d59f215ae3e208d0170073

注意:hashlib 加密啊的字符串類型為二進制編碼,直接加密字符串會報如下錯誤:    
sha1 = hashlib.sha1()
sha1.update(string)
res = sha1.hexdigest()
print("sha1加密結(jié)果:",res)
 
TypeError: Unicode-objects must be encoded before hashing

可以使用encode進行轉(zhuǎn)換

shaa1 = hashlib.sha1()
shaa1.update(string.encode('utf-8'))
res = shaa1.hexdigest()
print("sha1采用encode轉(zhuǎn)換加密結(jié)果:",res)

或者使用byte轉(zhuǎn)換為二進制    
shab1 = hashlib.sha1()
shab1.update(bytes(string,encoding='utf-8'))
res = shab1.hexdigest()
print("sha1采用byte轉(zhuǎn)換的結(jié)果:",res)

以上輸出:    
sha1采用encode轉(zhuǎn)換加密結(jié)果: 458d32be8ea38b66300174970ab0a8c0b734252f
sha1采用byte轉(zhuǎn)換的結(jié)果: 458d32be8ea38b66300174970ab0a8c0b734252f
常用方法
hash.update(arg) 更新哈希對象以字符串參數(shù), 注意:如果同一個hash對象重復(fù)調(diào)用該方法,則m.update(a); m.update(b) 等效于 m.update(a+b),看下面例子    
m = hashlib.md5()
m.update('a'.encode('utf-8'))
res = m.hexdigest()
print("第一次a加密:",res)
m.update('b'.encode('utf-8'))
res = m.hexdigest()
print("第二次b加密:",res)
 
m1 = hashlib.md5()
m1.update('b'.encode('utf-8'))
res = m1.hexdigest()
print("b單獨加密:",res)
m2 = hashlib.md5()
m2.update('ab'.encode('utf-8'))
res = m2.hexdigest()
print("ab單獨加密:",res)
 
輸出結(jié)果:
第一次a加密: 0cc175b9c0f1b6a831c399e269772661
第二次b加密: 187ef4436122d1cc2f40dc2b92f0eba0
b單獨加密: 92eb5ffee6ae2fec3ad71c777531578f
ab單獨加密: 187ef4436122d1cc2f40dc2b92f0eba0

hash.digest() 返回摘要,作為二進制數(shù)據(jù)字符串值,

hash.hexdigest() 返回摘要,作為十六進制數(shù)據(jù)字符串值,

hash.copy() 復(fù)制

高級加密
以上加密算法雖然依然非常厲害,但時候存在缺陷,即:通過撞庫可以反解。所以,有必要對加密算法中添加自定義key再來做加密。    
low = hashlib.md5()
low.update('ab'.encode('utf-8'))
res = low.hexdigest()
print("普通加密:",res)
high = hashlib.md5(b'beyondjie')
high.update('ab'.encode('utf-8'))
res = high.hexdigest()
print("采用key加密:",res)
輸出結(jié)果:
普通加密: 187ef4436122d1cc2f40dc2b92f0eba0
采用key加密: 1b073f6b8cffe609751e4c98537b7653

附加HMAC-SHA1各語言版本實現(xiàn)

在各大開放平臺大行其道的互聯(lián)網(wǎng)開發(fā)潮流中,調(diào)用各平臺的API接口過程中,無一例外都會用到計算簽名值(sig值)。而在各種計算簽名的方法中,經(jīng)常被采用的就是HMAC-SHA1,現(xiàn)對HMAC-SHA1做一個簡單的介紹:

        HMAC,散列消息鑒別碼,基于密鑰的Hash算法認證協(xié)議。實現(xiàn)原理為:利用已經(jīng)公開的Hash函數(shù)和私有的密鑰,來生成固定長度的消息鑒別碼;

       SHA1、MD5等Hash算法是比較常用的不可逆Hash簽名計算方法;

       BASE64,將任意序列的8字節(jié)字符轉(zhuǎn)換為人眼無法直接識別的符號編碼的一種方法;

       各個語言版本的實現(xiàn)為:

       Python版:    
import hmac
import hashlib
import base64
hmac.new(Token,data,hashlib.sha1).digest().encode('base64').rstrip()

Token:即接口的key

data:要加密的數(shù)據(jù)

       PHP版:    
base64_encode(hash_hmac("SHA1",clientStr,Token , true))

          C++版(Openssl):
    
HMAC( EVP_sha1(),
  /*key data*/ strKey.data(),
  /*key len*/ strKey.size(),
  /*data */(unsigned char*) strRandom.data(),
  /*data len*/ strRandom.size(), digest, &digest_len))

       Shell版:    
echo -n '3f88a95c532bea70' | openssl dgst -hmac '123' -sha1 -binary | base64
總結(jié)
以上就是本文關(guān)于python中hashlib模塊用法示例的全部內(nèi)容,希望對大家有所幫助。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }