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]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算