Skip to content

Instantly share code, notes, and snippets.

@sergiotapia
Last active August 19, 2025 09:37
Show Gist options
  • Select an option

  • Save sergiotapia/8263278 to your computer and use it in GitHub Desktop.

Select an option

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))
}
@r6m

r6m commented Jan 4, 2018

Copy link
Copy Markdown

+1 Thanks

@serboox

serboox commented Jan 12, 2018

Copy link
Copy Markdown

+1 Thanks!

@kj187

kj187 commented Jan 12, 2018

Copy link
Copy Markdown

+1 Thanks !!

@kisPocok

Copy link
Copy Markdown

<3

@lengyijun

Copy link
Copy Markdown

+1 Thanks !!

@jobezic

jobezic commented Mar 12, 2018

Copy link
Copy Markdown

+1

@elgrim312

elgrim312 commented May 4, 2018

Copy link
Copy Markdown

+1 thank !

@tonycai

tonycai commented May 10, 2018

Copy link
Copy Markdown

+1 thank !

@blueturtle13g

Copy link
Copy Markdown

cool! merci

@JohnDotOwl

Copy link
Copy Markdown

Thanks!

@sakishum

Copy link
Copy Markdown

+1 thank !

@krasnobay

Copy link
Copy Markdown

Thanks :)

@mag002

mag002 commented Nov 19, 2018

Copy link
Copy Markdown

+1 thanks!

@FrankIT60

Copy link
Copy Markdown

Thanks !

for big string I use:

import (
    "crypto/md5"
    "fmt"
)

func DigestString(s *string) string {
    return fmt.Sprintf("%x", md5.Sum([]byte(*s)))
}

@soyHouston256

Copy link
Copy Markdown

Thanks !

@bipin-mi

Copy link
Copy Markdown

I want to convert string to MD5 with base64 encoded string.
As I get that in PHP as like below.
But I need it in Golang

<?php
$str = "hello";
$str2 = mb_convert_encoding($str, "utf-8");
echo base64_encode(md5($str2,true));

Test url for PHP code "http://sandbox.onlinephpfunctions.com/code/e21dd185093817217427b6cd4e58a223e6ca3b27"
I tried many examples in Golang but didn't get success.

@drumer2142

Copy link
Copy Markdown

+1 Thanks

@MaximilianKlein

Copy link
Copy Markdown

+1 Thanks

@sergiotapia

sergiotapia commented Jul 17, 2019

Copy link
Copy Markdown
Author

Hey Sergio from 2014, you're not using Go anymore for years! You're now on Elixir for three years.

Sergio from 2029, are you an Elixir guru yet? Do you truly grok the Erlang's OTP and all the goodies it gives for free? Keep me posted!

@jjmartin

Copy link
Copy Markdown

👍 Bueno

@mustaqball

Copy link
Copy Markdown

+2 Thanks

@duzq

duzq commented Sep 16, 2019

Copy link
Copy Markdown

+1 Thanks

@DaniyarGilimov

Copy link
Copy Markdown

+1 Thanks from Kazakhstan

@bubunyo

bubunyo commented Apr 17, 2020

Copy link
Copy Markdown

@sergiotapia what is the importance of hex.EncodeToString?

@rodrigoarino

Copy link
Copy Markdown

@bipin-mi I have the same problem as you

@bipin-mi

bipin-mi commented May 13, 2020

Copy link
Copy Markdown

@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

uhhc commented Jun 18, 2021

Copy link
Copy Markdown

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

@micronull

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

@kewlamogh

Copy link
Copy Markdown

Really helpful, thanks!

@yechielb2000

Copy link
Copy Markdown

+1 Thanks from israel

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