Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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))
}
@wilsontamarozzi

This comment has been minimized.

Copy link

@wilsontamarozzi wilsontamarozzi commented Mar 26, 2016

Thank you, you helped me :)

@johnbuhay

This comment has been minimized.

Copy link

@johnbuhay johnbuhay commented Mar 27, 2016

+1 thank you

@skparwal

This comment has been minimized.

Copy link

@skparwal skparwal commented May 20, 2016

Thank you 👍 This is really helpful.

@jacygao

This comment has been minimized.

Copy link

@jacygao jacygao commented Jul 14, 2016

+1 thanks

@jde

This comment has been minimized.

Copy link

@jde jde commented Oct 14, 2016

This was helpful to me as well. Thanks from us over here at https://github.com/coralproject/shelf.

@itczl22

This comment has been minimized.

Copy link

@itczl22 itczl22 commented Dec 22, 2016

+1 thanks

@manoj150283

This comment has been minimized.

Copy link

@manoj150283 manoj150283 commented Jan 19, 2017

+1 thanks

@basement199

This comment has been minimized.

Copy link

@basement199 basement199 commented Jan 24, 2017

+1 thanks

@QuestionPython

This comment has been minimized.

Copy link

@QuestionPython QuestionPython commented Jan 31, 2017

good.
also write Decode of this.

@ahmdrz

This comment has been minimized.

Copy link

@ahmdrz ahmdrz commented Feb 22, 2017

@zhouhaibing089

This comment has been minimized.

Copy link

@zhouhaibing089 zhouhaibing089 commented Feb 23, 2017

Perhaps add hasher.Reset() at last.

@appins

This comment has been minimized.

Copy link

@appins appins commented Apr 26, 2017

Single most useful gist I've every seen....

@Passerby

This comment has been minimized.

Copy link

@Passerby Passerby commented Apr 27, 2017

+1 thanks

@Jolly23

This comment has been minimized.

Copy link

@Jolly23 Jolly23 commented May 7, 2017

+1 thanks

@cascad-s

This comment has been minimized.

Copy link

@cascad-s cascad-s commented May 18, 2017

  • 1 Man, you are a god! Thanks!
@darethas

This comment has been minimized.

Copy link

@darethas darethas commented Jul 20, 2017

a little bit shorter using just crypto/md5 and fmt

import (
    "crypto/md5"
    "fmt"
)

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

https://play.golang.org/p/66xZYQvlh_

@menuka94

This comment has been minimized.

Copy link

@menuka94 menuka94 commented Aug 1, 2017

+1 Thanks

@ZhenhangTung

This comment has been minimized.

Copy link

@ZhenhangTung ZhenhangTung commented Aug 2, 2017

+1 Thanks

@wwjue

This comment has been minimized.

Copy link

@wwjue wwjue commented Aug 13, 2017

thx

@praveenchilakanti

This comment has been minimized.

Copy link

@praveenchilakanti praveenchilakanti commented Sep 26, 2017

+1 thank you...

@silbinarywolf

This comment has been minimized.

Copy link

@silbinarywolf silbinarywolf commented Oct 22, 2017

+1 Thanks man!

@raven4752

This comment has been minimized.

Copy link

@raven4752 raven4752 commented Oct 24, 2017

short but helpful.Thanks man!

@businiaowyf

This comment has been minimized.

Copy link

@businiaowyf businiaowyf commented Dec 1, 2017

+1 Thanks!

@rezam90

This comment has been minimized.

Copy link

@rezam90 rezam90 commented Jan 4, 2018

+1 Thanks

@serboox

This comment has been minimized.

Copy link

@serboox serboox commented Jan 12, 2018

+1 Thanks!

@kj187

This comment has been minimized.

Copy link

@kj187 kj187 commented Jan 12, 2018

+1 Thanks !!

@kisPocok

This comment has been minimized.

Copy link

@kisPocok kisPocok commented Feb 26, 2018

<3

@lengyijun

This comment has been minimized.

Copy link

@lengyijun lengyijun commented Mar 1, 2018

+1 Thanks !!

@pinkra

This comment has been minimized.

Copy link

@pinkra pinkra commented Mar 12, 2018

+1

@elgrim312

This comment has been minimized.

Copy link

@elgrim312 elgrim312 commented May 4, 2018

+1 thank !

@tonycai

This comment has been minimized.

Copy link

@tonycai tonycai commented May 10, 2018

+1 thank !

@blueturtle13g

This comment has been minimized.

Copy link

@blueturtle13g blueturtle13g commented May 26, 2018

cool! merci

@rainbowhat

This comment has been minimized.

Copy link

@rainbowhat rainbowhat commented Jun 9, 2018

Thanks!

@sakishum

This comment has been minimized.

Copy link

@sakishum sakishum commented Jul 18, 2018

+1 thank !

@krasnobay

This comment has been minimized.

Copy link

@krasnobay krasnobay commented Nov 13, 2018

Thanks :)

@mag002

This comment has been minimized.

Copy link

@mag002 mag002 commented Nov 19, 2018

+1 thanks!

@FrankIT60

This comment has been minimized.

Copy link

@FrankIT60 FrankIT60 commented Dec 31, 2018

Thanks !

for big string I use:

import (
    "crypto/md5"
    "fmt"
)

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

This comment has been minimized.

Copy link

@ramirez456 ramirez456 commented Feb 3, 2019

Thanks !

@bipin-mi

This comment has been minimized.

Copy link

@bipin-mi bipin-mi commented Apr 12, 2019

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

This comment has been minimized.

Copy link

@drumer2142 drumer2142 commented Jun 7, 2019

+1 Thanks

@MaximilianKlein

This comment has been minimized.

Copy link

@MaximilianKlein MaximilianKlein commented Jun 26, 2019

+1 Thanks

@sergiotapia

This comment has been minimized.

Copy link
Owner Author

@sergiotapia sergiotapia commented Jul 17, 2019

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

This comment has been minimized.

Copy link

@jjmartin jjmartin commented Aug 16, 2019

👍 Bueno

@arman-mukatov

This comment has been minimized.

Copy link

@arman-mukatov arman-mukatov commented Sep 5, 2019

+2 Thanks

@duzq

This comment has been minimized.

Copy link

@duzq duzq commented Sep 16, 2019

+1 Thanks

@DaniyarGilimov

This comment has been minimized.

Copy link

@DaniyarGilimov DaniyarGilimov commented Nov 20, 2019

+1 Thanks from Kazakhstan

@bubunyo

This comment has been minimized.

Copy link

@bubunyo bubunyo commented Apr 17, 2020

@sergiotapia what is the importance of hex.EncodeToString?

@rodrigoarino

This comment has been minimized.

Copy link

@rodrigoarino rodrigoarino commented May 8, 2020

@bipin-mi I have the same problem as you

@bipin-mi

This comment has been minimized.

Copy link

@bipin-mi 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.