How to get a MD5 hash from a string in Golang?
Asked Answered
P

7

106

This is how I started to get a md5 hash from a string:

import "crypto/md5"

var original = "my string comes here"
var hash = md5.New(original)

But obviously this is not how it works. Can someone provide me a working sample for this?

Powe answered 4/3, 2010 at 8:47 Comment(1)
The very best answer to this question is in the comments by @Alexei Danchenkov below. I've implemented them in a quick runnable example here: play.golang.org/p/e7v_erP7GcKathrinekathryn
G
110

Reference Sum,For me,following work well:

package main

import (
    "crypto/md5"
    "fmt"
)

func main() {
    data := []byte("hello")
    fmt.Printf("%x", md5.Sum(data))
}
Grudge answered 14/1, 2015 at 12:9 Comment(0)
C
130
import (
    "crypto/md5"
    "encoding/hex"
)

func GetMD5Hash(text string) string {
   hash := md5.Sum([]byte(text))
   return hex.EncodeToString(hash[:])
}
Catalpa answered 13/8, 2014 at 12:59 Comment(3)
Well done! I personally like that your simple wrapping works 'transparently' in the sense that so many other programming languages do the same thing...Connecticut
what is the purpose of hash[:]? ThanksPolygon
@Madeo it means a slice which referencing the storage of (variable) hash, for the detail You can see here blog.golang.org/slices-introVoigt
G
110

Reference Sum,For me,following work well:

package main

import (
    "crypto/md5"
    "fmt"
)

func main() {
    data := []byte("hello")
    fmt.Printf("%x", md5.Sum(data))
}
Grudge answered 14/1, 2015 at 12:9 Comment(0)
G
43

I found this solution to work well

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
)

func main() {
    var str string = "hello world"

    hasher := md5.New()
    hasher.Write([]byte(str))
    fmt.Println(str)
    fmt.Println(hex.EncodeToString(hasher.Sum(nil)))
}
Govan answered 12/11, 2013 at 23:0 Comment(0)
U
38

From crypto/md5 doc:

package main

import (
    "crypto/md5"
    "fmt"
    "io"
)

func main() {
    h := md5.New()
    io.WriteString(h, "The fog is getting thicker!")
    fmt.Printf("%x", h.Sum(nil))
}
Unparalleled answered 4/3, 2010 at 11:52 Comment(5)
strings.Bytes doesn't exist anymore on -release, it's []byte(original)Baro
Looks like Hash.Sum() needs a byte slice as first argument now. h.Sum([]byte{}) will fix this example.Honniball
I have seen the documentation that this example is taken from but you haven't added anything to it, like an explanation for example. Why is io.WriteString() required? Why does h.Sum() require nil as an argument rather than taking the given string? A a GoNoob reading parroted examples is rather unedifying.Kafiristan
@IanLewis Writer io.WriteString() is unrelated to the subject. fmt.Fprintf(h, "The fog is getting thicker!") would produce the same result. Even clearer would be a one-liner fmt.Printf("%x\n", md5.Sum([]byte("The quick brown fox jumps over the lazy dog."))). h.Sum(in) called with any in would append the md5-hash of h to in - i.e. concatenate them (see the source, line 88: golang.org/src/pkg/crypto/md5/md5.go?s=2281:2313#L88).Karisakarissa
Well. I would like to get the string instead of printing it to the console O.o...Eidetic
U
19

I use this to MD5 hash my strings:

import (
    "crypto/md5"
    "encoding/hex"
)

func GetMD5Hash(text string) string {
    hasher := md5.New()
    hasher.Write([]byte(text))
    return hex.EncodeToString(hasher.Sum(nil))
}
Universalize answered 8/1, 2014 at 18:23 Comment(0)
Z
6

Here is a function you could use to generate an MD5 hash:

// MD5 hashes using md5 algorithm
func MD5(text string) string {
    algorithm := md5.New()
    algorithm.Write([]byte(text))
    return hex.EncodeToString(algorithm.Sum(nil))
}

I put together a group of those utility hash functions here: https://github.com/shomali11/util

You will find FNV32, FNV32a, FNV64, FNV65a, MD5, SHA1, SHA256 and SHA512

Zoophilia answered 12/11, 2017 at 0:33 Comment(1)
Well done, but your answer comes late... the same answer has already been provided three years before yours!Connecticut
T
0

just another answer

// MD5 hashes using md5 algorithm
func MD5(text string) string {
    data := []byte(text)
    return fmt.Sprintf("%x", md5.Sum(data))
}
Tillietillinger answered 13/2, 2022 at 9:56 Comment(3)
That's litterally the content of the accepted answer.Kassiekassity
Improved and simplified version :),Tillietillinger
Could you explain the difference between your answer and https://mcmap.net/q/203447/-how-to-get-a-md5-hash-from-a-string-in-golang ?Kassiekassity

© 2022 - 2024 — McMap. All rights reserved.