最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Python中计算字符串的md5值

Java crifan 213浏览 0评论
折腾:
【未解决】Python实现小花生中addSignature的md5加密生成签名的逻辑
期间,需要去找找Python中如何计算字符串的md5值。
python java md5
How to transplant the java MD5 encrypt code into Python? – Stack Overflow
MD5 hash in Python – GeeksforGeeks
# 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
去试试
python hashlib md5 digest方法的java实现-CSDN论坛
101 – Python Vs Java Md5 Hexdigest – pad.yohdah.com
https://pad.yohdah.com/101/python-vs-java-md5-hexdigest
【总结】
此处用代码:
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
继续参考:
How to transplant the java MD5 encrypt code into Python? – Stack Overflow
试试.digest()
结果:
md5Digest = md5Result.digest()
得到的却是byte的:
md5Digest=b'XZ\xd2v]\x14|~\x91\x84x\xa4\xce\x84>\xd2'
md5模块(Python内置模块)和hashlib模块 – duhaizhang的个人页面 – 开源中国
看来本身md5方法没啥问题。
怀疑是参数不对,所以继续研究参数。
然后去:
【已解决】为何Python中32字节的md值和小花生中getMD5Str计算出的md5值不同
后来证明上述计算方法是对的,只是参数问题而已。

转载请注明:在路上 » 【已解决】Python中计算字符串的md5值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.101 seconds, using 20.68MB memory