折腾:
【未解决】Python实现小花生中addSignature的md5加密生成签名的逻辑
期间,需要去找找Python中如何计算字符串的md5值。
python java md5
# Python 3 code to demonstrate the # working of MD5 (string - hexadecimal) import hashlib # initializing string str = "GeeksforGeeks" # encoding GeeksforGeeks using encode() # then sending to md5() result = hashlib.md5(str.encode()) # printing the equivalent hexadecimal value. print("The hexadecimal equivalent of hash is : ", end ="") print(result.hexdigest())
->
The hexadecimal equivalent of hash is : f1e069787ece74531d112559945c6871
去试试
101 – Python Vs Java Md5 Hexdigest – pad.yohdah.com
【总结】
此处用代码:
from hashlib import md5 def generateSignature(timestampInt, paramString): userId = "1134723" timestamp = "%s" % timestampInt localObject = "/Reading.svc/parentChildReadingBookQuery2" fixedSault = “AyGt7ohMR!xx#N" strToCalc = userId + timestamp + localObject + paramString + fixedSault print("strToCalc=%s" % strToCalc) encodedStr = strToCalc.encode() # encodedStr = strToCalc.encode("UTF-8") print("encodedStr=%s" % encodedStr) md5Result = md5(encodedStr) print("md5Result=%s" % md5Result) # md5Result=<md5 HASH object @ 0x1044f1df0> md5Hexdigest = md5Result.hexdigest() print("md5Hexdigest=%s" % md5Hexdigest) # md5Hexdigest=585ad2765d147c7e918478a4ce843ed2 return md5Hexdigest if __name__ == "__main__": timestampInt = 1553845899 paramString = "{\"userId\":\"1134723\",\"fieldName\":\"\",\"fieldValue\":\"全部类别\",\"theStageOfTheChild\":\"\",\"parentalEnglishLevel\":\"\",\"supportingResources\":\"有音频\",\"offset\":20,\"limit\":10}" generatedSignature = generateSignature(timestampInt, paramString) print("timestampInt=%d, paramString=%s-> %s" % (timestampInt, paramString, generatedSignature))
是可以生成md5值的:
timestampInt=1553845899, paramString={"userId":"1134723","fieldName":"","fieldValue":"全部类别","theStageOfTheChild":"","parentalEnglishLevel":"","supportingResources":"有音频","offset":20,"limit":10}-> 585ad2765d147c7e918478a4ce843ed2
【后记】
但是生成的值是:
585ad2765d147c7e918478a4ce843ed2
不是希望的:
c687d5dfa015246e6bdc6b3c27c2afea
继续参考:
试试.digest()
结果:
md5Digest = md5Result.digest()
得到的却是byte的:
md5Digest=b'XZ\xd2v]\x14|~\x91\x84x\xa4\xce\x84>\xd2'
看来本身md5方法没啥问题。
怀疑是参数不对,所以继续研究参数。
然后去:
【已解决】为何Python中32字节的md值和小花生中getMD5Str计算出的md5值不同
后来证明上述计算方法是对的,只是参数问题而已。
转载请注明:在路上 » 【已解决】Python中计算字符串的md5值