Search code examples
arraysstringgocasting

GO Weirdness Converting Btye array to string from MD5 hash


Could someone enlighten me where I'm going wrong.

I can't convert the byte array produced from the hash sum function by casting with string, I have to use Sprintf.

Here is a snippet of the code:

f, _ := os.Open(filename)
hash := md5.New()
io.Copy(hash, f)
hashStringGood := fmt.Sprintf("%x", hash.Sum(nil))
hashStringJunk := string(hash.Sum(nil)[:])

hasStringGood would result in d41d8cd98f00b204e9800998ecf8427e hashStringJunk would result in ��ُ�� ���B~


Solution

  • When you convert random binary data to a string without an encoding scheme, it's very unlikely that the data will map to a sequence of printable characters.

    The %x verb from the fmt package is a convenience method for hex-encoding binary data. From the "String and slice of bytes" section of the verb definitions in the fmt package documentation:

    %s  the uninterpreted bytes of the string or slice
    %q  a double-quoted string safely escaped with Go syntax
    %x  base 16, lower-case, two characters per byte
    

    Alternatively, you can use packages nested underneath the encoding package to encode the data:

    package main
    
    import (
        "crypto/md5"
        "encoding/base64"
        "encoding/hex"
        "fmt"
    )
    
    func main() {
        hash := md5.Sum([]byte("input to be hashed"))
        fmt.Printf("Using %%s verb: %s\n", hash)
        fmt.Printf("Using %%q verb: %q\n", hash)
        fmt.Printf("Using %%x verb: %x\n", hash)
    
        hexHash := hex.EncodeToString(hash[:])
        fmt.Printf("Converted to a hex-encoded string: %s\n", hexHash)
    
        base64Hash := base64.StdEncoding.EncodeToString(hash[:])
        fmt.Printf("Converted to a base64-encoded string: %s\n", base64Hash)
    }
    

    Output

    Using %s verb: �����Q���6���5�
    Using %q verb: "\x8d\xa8\xf1\xf8\x06\xd3Q\x9d\xa1\xe46\xdb\xfb\x9f5\xd7"
    Using %x verb: 8da8f1f806d3519da1e436dbfb9f35d7
    Converted to a hex-encoded string: 8da8f1f806d3519da1e436dbfb9f35d7
    Converted to a base64-encoded string: jajx+AbTUZ2h5Dbb+5811w==
    

    Go Playground