为了账号安全,请及时绑定邮箱和手机立即绑定

checksum of a byte array

checksum of a byte array

Go
至尊宝的传说 2022-10-04 19:50:29
我有一个由字符串制成的 ,:[]byte    array := []byte("some string")它看起来像预期:    [115 111 109 101 32 115 116 114 105 110 103]有没有办法简单地获取 [] 字节的校验和?喜欢:    sum(array)
查看完整描述

3 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

也许你需要md5.sum来检查可靠性。


https://pkg.go.dev/crypto/md5#Sum


package main


import (

    "crypto/md5"

    "fmt"

)


func main() {

    data := []byte("some string")

    fmt.Printf("%x", md5.Sum(data))

}

另一个例子。


https://play.golang.org/p/7_ctunsqHS3


查看完整回答
反对 回复 2022-10-04
?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

我认为尽可能避免使用转换是件好事:fmt


package main


import (

   "crypto/md5"

   "encoding/hex"

)


func checksum(s string) string {

   b := md5.Sum([]byte(s))

   return hex.EncodeToString(b[:])

}


func main() {

   s := checksum("some string")

   println(s == "5ac749fbeec93607fc28d666be85e73a")

}

https://godocs.io/crypto/md5#Sum


查看完整回答
反对 回复 2022-10-04
?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

package main


import (

    "crypto/md5"

    "encoding/hex"

    "fmt"

)


func GetMD5HashWithSum(text string) string {

    hash := md5.Sum([]byte(text))

    return hex.EncodeToString(hash[:])

}


func main() {

    hello := GetMD5HashWithSum("some string")

    fmt.Println(hello)

}


查看完整回答
反对 回复 2022-10-04
  • 3 回答
  • 0 关注
  • 64 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信