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 commented Mar 26, 2016

Thank you, you helped me :)

@johnbuhay

This comment has been minimized.

Copy link

johnbuhay commented Mar 27, 2016

+1 thank you

@skparwal

This comment has been minimized.

Copy link

skparwal commented May 20, 2016

Thank you 👍 This is really helpful.

@JacyGao

This comment has been minimized.

Copy link

JacyGao commented Jul 14, 2016

+1 thanks

@jde

This comment has been minimized.

Copy link

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 commented Dec 22, 2016

+1 thanks

@manoj150283

This comment has been minimized.

Copy link

manoj150283 commented Jan 19, 2017

+1 thanks

@basement199

This comment has been minimized.

Copy link

basement199 commented Jan 24, 2017

+1 thanks

@QuestionPython

This comment has been minimized.

Copy link

QuestionPython commented Jan 31, 2017

good.
also write Decode of this.

@ahmdrz

This comment has been minimized.

Copy link

ahmdrz commented Feb 22, 2017

@zhouhaibing089

This comment has been minimized.

Copy link

zhouhaibing089 commented Feb 23, 2017

Perhaps add hasher.Reset() at last.

@appins

This comment has been minimized.

Copy link

appins commented Apr 26, 2017

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

@Passerby

This comment has been minimized.

Copy link

Passerby commented Apr 27, 2017

+1 thanks

@Jolly23

This comment has been minimized.

Copy link

Jolly23 commented May 7, 2017

+1 thanks

@cascad-s

This comment has been minimized.

Copy link

cascad-s commented May 18, 2017

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

This comment has been minimized.

Copy link

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 commented Aug 1, 2017

+1 Thanks

@ZhenhangTung

This comment has been minimized.

Copy link

ZhenhangTung commented Aug 2, 2017

+1 Thanks

@wwjue

This comment has been minimized.

Copy link

wwjue commented Aug 13, 2017

thx

@praveenchilakanti

This comment has been minimized.

Copy link

praveenchilakanti commented Sep 26, 2017

+1 thank you...

@silbinarywolf

This comment has been minimized.

Copy link

silbinarywolf commented Oct 22, 2017

+1 Thanks man!

@raven4752

This comment has been minimized.

Copy link

raven4752 commented Oct 24, 2017

short but helpful.Thanks man!

@businiaowyf

This comment has been minimized.

Copy link

businiaowyf commented Dec 1, 2017

+1 Thanks!

@rezam90

This comment has been minimized.

Copy link

rezam90 commented Jan 4, 2018

+1 Thanks

@serboox

This comment has been minimized.

Copy link

serboox commented Jan 12, 2018

+1 Thanks!

@kj187

This comment has been minimized.

Copy link

kj187 commented Jan 12, 2018

+1 Thanks !!

@kisPocok

This comment has been minimized.

Copy link

kisPocok commented Feb 26, 2018

<3

@lengyijun

This comment has been minimized.

Copy link

lengyijun commented Mar 1, 2018

+1 Thanks !!

@pinkra

This comment has been minimized.

Copy link

pinkra commented Mar 12, 2018

+1

@elgrim312

This comment has been minimized.

Copy link

elgrim312 commented May 4, 2018

+1 thank !

@tonycai

This comment has been minimized.

Copy link

tonycai commented May 10, 2018

+1 thank !

@blueturtle13g

This comment has been minimized.

Copy link

blueturtle13g commented May 26, 2018

cool! merci

@rainbowhat

This comment has been minimized.

Copy link

rainbowhat commented Jun 9, 2018

Thanks!

@sakishum

This comment has been minimized.

Copy link

sakishum commented Jul 18, 2018

+1 thank !

@krasnobay

This comment has been minimized.

Copy link

krasnobay commented Nov 13, 2018

Thanks :)

@mag002

This comment has been minimized.

Copy link

mag002 commented Nov 19, 2018

+1 thanks!

@FrankIT60

This comment has been minimized.

Copy link

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 commented Feb 3, 2019

Thanks !

@bipin-mi

This comment has been minimized.

Copy link

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 commented Jun 7, 2019

+1 Thanks

@MaximilianKlein

This comment has been minimized.

Copy link

MaximilianKlein commented Jun 26, 2019

+1 Thanks

@sergiotapia

This comment has been minimized.

Copy link
Owner Author

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 commented Aug 16, 2019

👍 Bueno

@arman-mukatov

This comment has been minimized.

Copy link

arman-mukatov commented Sep 5, 2019

+2 Thanks

@duzq

This comment has been minimized.

Copy link

duzq commented Sep 16, 2019

+1 Thanks

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.