Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate sha256 file checksum in Go

Tags:

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.

like image 637
alpav Avatar asked Apr 08 '13 12:04

alpav


People also ask

How do you find the checksum of a file?

Open a terminal window. Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file.

What is the SHA256 sum of a file?

What is SHA256 Checksum? It is a cryptographic signature of any file that it can use to identify or detect errors introduced during transmission or storage. A checksum measures the size of a block of digital data. It uniquely represents a file that is a string.

How is hash file calculated?

In Windows File Explorer select the files you want the hash values calculated for, click the right mouse button, and select Calculate Hash Value, then select the appropriate hash type from the pop-up sub-menu (e.g. MD5). The values will then be calculated and displayed.


2 Answers

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)) } 
like image 84
Melvin Avatar answered Sep 20 '22 05:09

Melvin


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) } 
like image 34
James Henstridge Avatar answered Sep 18 '22 05:09

James Henstridge