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=×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)