python b64encode_python base64编码解码、SHA256编码、urlsafe_b64encode编码

这篇博客介绍了如何在Python中使用hashlib库进行MD5、SHA系列的编码,以及使用base64库进行base64编码和解码。还涉及到urlsafe_b64encode编码,并展示了SHA256编码的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

importbase64importhmacimporthashlib#MD5 编码 应用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前要进行编码需要转为gb2312#这样才可能跟其他工具的编码一样(当然具体转为那种编码,前后端需要统一即可)

user= ‘张三as‘pwd= ‘a小四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小四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型的,需要转化为str

base64String =base64String.decode()print("base64String.decode :", base64String) #现在 才是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()#把小写转换为大写

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=&timestamp=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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值