5

golang中字符串MD5生成方式

 3 years ago
source link: https://studygolang.com/articles/35074
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

golang中字符串MD5生成方式

hduhuximing · 大约21小时之前 · 113 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    
func md5V(str string) string  {
    h := md5.New()
    h.Write([]byte(str))
    return hex.EncodeToString(h.Sum(nil))
}
func md5V2(str string) string {
    data := []byte(str)
    has := md5.Sum(data)
    md5str := fmt.Sprintf("%x", has)
    return md5str
}
func md5V3(str string) string {
    w := md5.New()
    io.WriteString(w, str)
    md5str := fmt.Sprintf("%x", w.Sum(nil))
    return md5str
}

整体测试代码

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io"
)
func main() {
    str := "MD5testing"
    md5Str := md5V(str)
    fmt.Println(md5Str)
    fmt.Println(md5V2(str))
    fmt.Println(md5V3(str))
}
// 输出结果:
f7bb96d1dcd6cfe0e5ce1f03e35f84bf
f7bb96d1dcd6cfe0e5ce1f03e35f84bf
f7bb96d1dcd6cfe0e5ce1f03e35f84bf

文章来源:https://www.jianshu.com/p/58dcbf490ef3


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK