101

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?

Rene Knop
  • 1,788
  • 3
  • 15
  • 27
cringe
  • 13,401
  • 15
  • 69
  • 102
  • 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: https://play.golang.org/p/e7v_erP7Gc – Aaron Jul 07 '16 at 13:02

7 Answers7

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

func GetMD5Hash(text string) string {
   hash := md5.Sum([]byte(text))
   return hex.EncodeToString(hash[:])
}
aviv
  • 1,673
  • 1
  • 13
  • 16
  • 1
    Well done! I personally like that your simple wrapping works 'transparently' in the sense that so many other programming languages do the same thing... – Gwyneth Llewelyn Jun 26 '20 at 16:01
  • 1
    what is the purpose of `hash[:]`? Thanks – Matteo Oct 05 '20 at 03:11
  • 3
    @Madeo it means a slice which referencing the storage of (variable) `hash`, for the detail You can see here https://blog.golang.org/slices-intro – Sal Prima Oct 20 '20 at 15:32
107

Reference Sum,For me,following work well:

package main

import (
    "crypto/md5"
    "fmt"
)

func main() {
    data := []byte("hello")
    fmt.Printf("%x", md5.Sum(data))
}
Alan
  • 1,691
  • 1
  • 16
  • 13
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)))
}
user387049
  • 6,647
  • 8
  • 53
  • 55
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))
}
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
Stephen Hsu
  • 5,127
  • 7
  • 31
  • 39
  • 3
    strings.Bytes doesn't exist anymore on -release, it's []byte(original) – marketer Mar 04 '10 at 12:42
  • 6
    Looks like `Hash.Sum()` needs a byte slice as first argument now. `h.Sum([]byte{})` will fix this example. – Philippe Gerber Apr 08 '12 at 16:13
  • 12
    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. – Ian Lewis Jan 28 '14 at 11:31
  • @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: http://golang.org/src/pkg/crypto/md5/md5.go?s=2281:2313#L88). – Alexei Danchenkov Mar 08 '14 at 01:38
  • 1
    Well. I would like to get the string instead of printing it to the console O.o... – C4d Oct 18 '18 at 14:14
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))
}
sergserg
  • 21,716
  • 41
  • 129
  • 182
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

Raed Shomali
  • 1,385
  • 15
  • 7
1

just another answer

// MD5 hashes using md5 algorithm
func MD5(text string) string {
    data := []byte(text)
    return fmt.Sprintf("%x", md5.Sum(data))
}
Yuseferi
  • 7,931
  • 11
  • 67
  • 103