How to calculate sha256 file checksum in Go

34,680

Solution 1

The SHA256 hasher implements the io.Writer interface, so one option would be to use the io.Copy() function to copy the data from an appropriate io.Reader in blocks. Something like this should do:

f, err := os.Open(os.Args[1])
if err != nil {
    log.Fatal(err)
}
defer f.Close()
if _, err := io.Copy(hasher, f); err != nil {
    log.Fatal(err)
}

Solution 2

The crypto/sha256 godoc actually has a snippet that shows how to do that (it's basically the same code as James):

package main

import (
  "crypto/sha256"
  "fmt"
  "io"
  "log"
  "os"
)

func main() {
  f, err := os.Open("file.txt")
  if err != nil {
    log.Fatal(err)
  }
  defer f.Close()

  h := sha256.New()
  if _, err := io.Copy(h, f); err != nil {
    log.Fatal(err)
  }

  fmt.Printf("%x", h.Sum(nil))
}

Solution 3

Full example of md5sum:

func md5sum(filePath string) (string, error) {
    file, err := os.Open(filePath)
    if err != nil {
        return "", err
    }
    defer file.Close()

    hash := md5.New()
    if _, err := io.Copy(hash, file); err != nil {
        return "", err
    }
    return hex.EncodeToString(hash.Sum(nil)), nil
}

EncodeToString does not omits leading 0 bytes, so fmt.Println(hex.EncodeToString([]byte{0x00, 0x00, 0xA, 0xB, 0xC})) gives 00000a0b0c

Share:
34,680
alpav
Author by

alpav

Email me to [email protected] and put so8350 anywhere in subject Phone: (667) 444-2111

Updated on November 08, 2020

Comments

  • alpav
    alpav over 3 years

    I need utility for Windows that calculates sha256 file checksum so that when I download fedora I can verify checksum from here: https://fedoraproject.org/static/checksums/Fedora-18-i386-CHECKSUM

    Microsoft utility from http://support.microsoft.com/kb/889768 does only md5 and sha1.

    I don't want to use other downloadable tools that are not signed and not available from https or from sources that I don't know about, because it does not make any sense to download unsigned code over unencrypted connection or from untrusted source to verify signature of another code to trust it.

    Luckily google provides possibility to use https for all downloads so I can download Go over secure connection and start from there.

    Here is simple code that does that for a small file, but it's not very good for big files because it's not streaming.

    package main
    
    import (
        "io/ioutil"
        "crypto/sha256"
        "os"
        "log"
        "encoding/hex"
    )
    
    func main() {
        hasher := sha256.New()
        s, err := ioutil.ReadFile(os.Args[1])    
        hasher.Write(s)
        if err != nil {
            log.Fatal(err)
        }
    
        os.Stdout.WriteString(hex.EncodeToString(hasher.Sum(nil)))
    }
    

    How to make it to use streams so that it works on any file size.