python b64encode_python base64编码解码、SHA256编码、urlsafe_b64encode编码
importbase64importhmacimporthashlib#MD5 編碼 應(yīng)用haslib
user = ‘username‘pwd= ‘pass123456‘user= user.encode(encoding=‘utf-8‘)
pwd= pwd.encode(encoding=‘utf-8‘)
user_MD5=hashlib.md5(user).hexdigest()
pwd_MD5=hashlib.md5(pwd).hexdigest()print(‘user_MD5:‘, user_MD5)print(‘pwd_MD5:‘, pwd_MD5)#MD5 編碼含有中文#如果有中文中文字符在Python中是以unicode存在的,同一個字符串在不同的編碼體系下有不同的值,所以在hash前要進(jìn)行編碼需要轉(zhuǎn)為gb2312#這樣才可能跟其他工具的編碼一樣(當(dāng)然具體轉(zhuǎn)為那種編碼,前后端需要統(tǒng)一即可)
user= ‘張三as‘pwd= ‘a(chǎn)小四a‘user= user.encode(encoding=‘gb2312‘)
pwd= pwd.encode(encoding=‘utf-8‘)
user_MD5=hashlib.md5(user).hexdigest()
pwd_MD5=hashlib.md5(pwd).hexdigest()print(‘張三as:‘, user_MD5)print(‘a(chǎn)小四a:‘, pwd_MD5)#hashlib的編碼:md5 sha1 sha3_224 sha3_256 sha3_384 sha3_512 sha224 sha384 sha512 shake_128 shake_256
a = "hello word"a= a.encode(encoding=‘utf-8‘)print(‘hello word:md5 =‘, hashlib.md5(a).hexdigest())print(‘hello word:sha1 =‘, hashlib.sha1(a).hexdigest())print(‘hello word:sha224 =‘, hashlib.sha224(a).hexdigest())print(‘hello word:sha256 =‘, hashlib.sha256(a).hexdigest())print(‘hello word:sha384 =‘, hashlib.sha384(a).hexdigest())print(‘hello word:sha512 =‘, hashlib.sha512(a).hexdigest())#base64 編碼
string = ‘helloWord‘byteString= string.encode(encoding=‘utf-8‘)
base64String=base64.b64encode(byteString)print("base64String :", base64String) #這個時候base64String 是byte型的,需要轉(zhuǎn)化為str
base64String =base64String.decode()print("base64String.decode :", base64String) #現(xiàn)在 才是str型的
#base64 解碼
decodestr=base64.b64decode(base64String)print(‘decodestr:‘, decodestr)print("decodestr.decode:", decodestr.decode())#SHA256編碼
string = ‘123456‘byteString= string.encode(encoding=‘utf-8‘)print(byteString)
sha256str=hashlib.sha256(byteString).hexdigest()#把小寫轉(zhuǎn)換為大寫
sha256str =sha256str.upper()print("sha256str:", sha256str)#urlsafe_b64encode編碼
p= ‘PUT‘m= ‘\n‘q= "/api/v1/t11104_1502526876337/status";
s= "api_sign_key";
qs= "timestamp=1502526886275";
pay= "{\"device\":{\"app_version_number\":12,\"dtype\":1,\"did\":\"2c6e2d7594e49a4a\",\"net_type\":\"WIFI\",\"system_version_name\":\"4.1.1\",\"app_version_name\":\"1.0.2\",\"channel\":\"200\",\"lang\":\"zh\",\"phone_model\":\"Samsung Galaxy S2 - 4.1.1 - API 16 - 480x800\",\"country\":\"US\"}},\"previous_status\":\"CREATED\",\"status_to_change\":\"LIVING\"";
secret_key= s.encode(encoding=‘utf-8‘)
message= (p+m+q+m+qs+m+pay).encode(encoding=‘utf-8‘)print(‘p+m+q+m+qs+m+pay:‘, p+m+q+m+qs+m+pay)print(‘byte_secret_key:‘, secret_key)print(‘byte_message:‘, message)#這里舉例 sha256編碼 除此之外,hmac 也有其他的各種編碼:#md5 sha1 sha3_224 sha3_256 sha3_384 sha3_512 sha224 sha384 sha512 shake_128 shake_256
digest = hmac.new(secret_key, message, digestmod=hashlib.sha256).digest()print(‘digest:‘, digest)
sig=base64.urlsafe_b64encode(digest).decode()print(‘sig‘, sig)
sig= sig.rstrip(‘=‘)print(‘sig去除末尾=號‘, sig)
p= ‘GET‘m= ‘\n‘q= "/api/v1/discovery";
s= "api_sign_key";
qs= "device=eyJhcHBfdmVyc2lvbl9udW1iZXIiOi0xMDAwLCJkdHlwZSI6MSwiZGlkIjoiMmM2ZTJkNzU5NGU0OWE0YSIsIm5ldF90eXBlIjoiV0lGSSIsInN5c3RlbV92ZXJzaW9uX25hbWUiOiI0LjEuMSIsImFwcF92ZXJzaW9uX25hbWUiOiIxLjAuMiIsImNoYW5uZWwiOiIyMDAiLCJsYW5nIjoiemgiLCJwaG9uZV9tb2RlbCI6IlNhbXN1bmcgR2FsYXh5IFMyIC0gNC4xLjEgLSBBUEkgMTYgLSA0ODB4ODAwIiwiY291bnRyeSI6IlVTIn0=&size=50&last_seen_pos=×tamp=1508382408763";
pay= ""secret_key= s.encode(encoding=‘utf-8‘)
message= (p+m+q+m+qs+m+pay).encode(encoding=‘utf-8‘)print(‘p+m+q+m+qs+m+pay:‘, p+m+q+m+qs+m+pay)print(‘byte_secret_key:‘, secret_key)print(‘byte_message:‘, message)
digest= hmac.new(secret_key, message, digestmod=hashlib.sha256).digest()print(‘digest:‘, digest)
sig=base64.urlsafe_b64encode(digest).decode()print(‘sig‘, sig)
sig= sig.rstrip(‘=‘)print(‘sig去除末尾=號‘, sig)
總結(jié)
以上是生活随笔為你收集整理的python b64encode_python base64编码解码、SHA256编码、urlsafe_b64encode编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab进行预测误差过大,神经网络预
- 下一篇: websocket python爬虫_p