Skip to content

Instantly share code, notes, and snippets.

@sergiotapia
Last active December 5, 2023 03:53
Show Gist options
  • Save sergiotapia/8263278 to your computer and use it in GitHub Desktop.
Save sergiotapia/8263278 to your computer and use it in GitHub Desktop.
Golang - How to hash a string using MD5.
import (
"crypto/md5"
"encoding/hex"
)
func GetMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}
@DaniyarGilimov
Copy link

+1 Thanks from Kazakhstan

@bubunyo
Copy link

bubunyo commented Apr 17, 2020

@sergiotapia what is the importance of hex.EncodeToString?

@rodrigoarino
Copy link

@bipin-mi I have the same problem as you

@bipin-mi
Copy link

bipin-mi commented May 13, 2020

@bipin-mi I have the same problem as you

I get solution with below code snippet

str := "hello"
hasher := md5.New()
hasher.Write([]byte(str))
encodedString := b64.StdEncoding.EncodeToString(hasher.Sum(nil))
fmt.Println(encodedString)

@uhhc
Copy link

uhhc commented Jun 18, 2021

Please see the benchmark of different methods for generating MD5:https://play.golang.org/p/9bAidOZFgFt

@micronull
Copy link

func makeMD5(in string) string {
	binHash := md5.Sum([]byte(in))
	return hex.EncodeToString(binHash[:])
}

@kewlamogh
Copy link

Really helpful, thanks!

@bugielektrik
Copy link

+1 Thanks from Kazakhstan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment