如何在Golang中从字符串获取MD5哈希值?

huangapple go评论92阅读模式
英文:

How to get a MD5 hash from a string in Golang?

问题

这是我开始从一个字符串获取md5哈希值的方法:

import "crypto/md5"

var original = "我的字符串在这里"
var hash = md5.New(original)

但显然这不是正确的方法。有人能提供一个可行的示例吗?

英文:

This is how I started to get a md5 hash from a string:

import "crypto/md5"

var original = "my string comes here"
var hash = md5.New(original)

But obviously this is not how it works. Can someone provide me a working sample for this?

答案1

得分: 120

import (
"crypto/md5"
"encoding/hex"
)

func GetMD5Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}

英文:
import (
    "crypto/md5"
    "encoding/hex"
)

func GetMD5Hash(text string) string {
   hash := md5.Sum([]byte(text))
   return hex.EncodeToString(hash[:])
}

答案2

得分: 107

参考Sum,对我来说,以下代码运行良好:

package main

import (
    "crypto/md5"
    "fmt"
)

func main() {
    data := []byte("hello")
    fmt.Printf("%x", md5.Sum(data))
}
英文:

Reference Sum,For me,following work well:

package main

import (
	"crypto/md5"
	"fmt"
)

func main() {
	data := []byte("hello")
	fmt.Printf("%x", md5.Sum(data))
}

答案3

得分: 43

我发现这个解决方案很有效

package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
)

func main() {
	var str string = "你好世界"

	hasher := md5.New()
	hasher.Write([]byte(str))
	fmt.Println(str)
	fmt.Println(hex.EncodeToString(hasher.Sum(nil)))
}
英文:

I found this solution to work well

package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
)

func main() {
	var str string = "hello world"

	hasher := md5.New()
	hasher.Write([]byte(str))
	fmt.Println(str)
	fmt.Println(hex.EncodeToString(hasher.Sum(nil)))
}

答案4

得分: 38

crypto/md5文档中:

package main

import (
	"crypto/md5"
	"fmt"
	"io"
)

func main() {
	h := md5.New()
	io.WriteString(h, "The fog is getting thicker!")
	fmt.Printf("%x", h.Sum(nil))
}
英文:

From crypto/md5 doc:

package main

import (
	"crypto/md5"
	"fmt"
	"io"
)

func main() {
	h := md5.New()
	io.WriteString(h, "The fog is getting thicker!")
	fmt.Printf("%x", h.Sum(nil))
}

答案5

得分: 19

我用这个来对我的字符串进行MD5哈希:

import (
    "crypto/md5"
    "encoding/hex"
)
 
func GetMD5Hash(text string) string {
    hasher := md5.New()
    hasher.Write([]byte(text))
    return hex.EncodeToString(hasher.Sum(nil))
}
英文:

I use this to MD5 hash my strings:

import (
    "crypto/md5"
    "encoding/hex"
)
 
func GetMD5Hash(text string) string {
    hasher := md5.New()
    hasher.Write([]byte(text))
    return hex.EncodeToString(hasher.Sum(nil))
}

答案6

得分: 6

这里是一个你可以用来生成MD5哈希值的函数:

// 使用md5算法进行MD5哈希
func MD5(text string) string {
    algorithm := md5.New()
    algorithm.Write([]byte(text))
    return hex.EncodeToString(algorithm.Sum(nil))
}

我在这里组合了一组实用的哈希函数:https://github.com/shomali11/util

你会找到 FNV32, FNV32a, FNV64, FNV65a, MD5, SHA1, SHA256SHA512

英文:

Here is a function you could use to generate an MD5 hash:

// MD5 hashes using md5 algorithm
func MD5(text string) string {
    algorithm := md5.New()
	algorithm.Write([]byte(text))
    return hex.EncodeToString(algorithm.Sum(nil))
}

I put together a group of those utility hash functions here: https://github.com/shomali11/util

You will find FNV32, FNV32a, FNV64, FNV65a, MD5, SHA1, SHA256 and SHA512

答案7

得分: 2

只是另一个答案

// 使用md5算法进行MD5哈希
func MD5(text string) string {
    data := []byte(text)
    return fmt.Sprintf("%x", md5.Sum(data))
}
英文:

just another answer

// MD5 hashes using md5 algorithm
func MD5(text string) string {
	data := []byte(text)
	return fmt.Sprintf("%x", md5.Sum(data))
}

huangapple
  • 本文由 发表于 2010年3月4日 16:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/2377881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定