doulian1852 2017-10-18 06:41
浏览 155

如何在Python中类似于Go中的“ crypto / md5”来计算字符串的md5

I know there is hashlib in Python, but I want to achieve the same result as in Go below:

package main

import (
    "crypto/md5"
    "fmt"
)

func main() {
    data := []byte("12345")
    fmt.Println("sum ", md5.Sum(data))
}

As func md5.Sum described, it calculates "MD5 checksum of the data". However, I don't find any similar function in Python.

Is there any way to achieve md5.Sum in Python as in Go?

The output of program above is a slice other than a string:

sum  [32 44 185 98 172 89 7 91 150 75 7 21 45 35 75 112]
  • 写回答

1条回答 默认 最新

  • doushan15559 2017-10-18 08:10
    关注

    Based on PM 2Ring's solution, here is a working program,

    from hashlib import md5
    
    hexv = md5(b'12345').hexdigest()
    l = [str(int(i+j,16)) for i, j in zip(hexv[::2], hexv[1::2])]
    print("sum [" + ", ".join(l)+"]")
    

    This prints,

    sum [130, 124, 203, 14, 234, 138, 112, 108, 76, 52, 161, 104, 145, 248, 78, 123]
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮