Last active
March 31, 2025 17:28
-
Star
(138)
You must be signed in to star a gist -
Fork
(27)
You must be signed in to fork a gist
-
-
Save sergiotapia/8263278 to your computer and use it in GitHub Desktop.
Golang - How to hash a string using MD5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"crypto/md5" | |
"encoding/hex" | |
) | |
func GetMD5Hash(text string) string { | |
hasher := md5.New() | |
hasher.Write([]byte(text)) | |
return hex.EncodeToString(hasher.Sum(nil)) | |
} |
@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)
Please see the benchmark of different methods for generating MD5:https://play.golang.org/p/9bAidOZFgFt
func makeMD5(in string) string {
binHash := md5.Sum([]byte(in))
return hex.EncodeToString(binHash[:])
}
Really helpful, thanks!
+1 Thanks from Kazakhstan
+1 Thanks from israel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bipin-mi I have the same problem as you