Hi. I'm trying to learn Go after many years of using a different
language. My development machine runs Ubuntu 16.04 amd64, go 1.6.2
linux/amd
I compared the output of the following code, and my output differs from the
std utilities such as sha1sum, sha215sum, etc. I used
go1.6.3.windows-386.msi as my test file.
I also noticed that the hashes that were output were different, depending
on the value I set for my ReadBufferSize. I don't understand why the
output I'm seeing doesn't match the std utilities, and why the hashes vary
w/ my buffer size.
Thanks,
Rob Solomon
package main;
import (
"os"
"fmt"
"runtime"
"encoding/hex"
"crypto/sha512"
"crypto/sha256"
"crypto/sha1"
"crypto/md5"
"io"
"hash"
"getcommandline" // this one's mine.
)
func main() {
const K = 1024;
const M = 1024*1024;
const (
md5hash = iota
sha1hash
sha256hash
sha384hash
sha512hash
HashType
);
const ReadBufferSize = 10 * M;
var HashName = [...]string{"md5","sha1","sha256","sha384","sha512"};
var WhichHash int;
var hasher hash.Hash;
var FileSize int64;
if len(os.Args) <= 1 {
fmt.Println(" Need input filename as a param. ");
os.Exit(0);
}
FileToHash := getcommandline.GetCommandLineString();
fmt.Println();
fmt.Print(" GOOS =",runtime.GOOS,". ARCH=",runtime.GOARCH);
fmt.Println(" WhichHash = ",HashName[WhichHash]);
fmt.Println();
fmt.Println();
for {
FileSize = 0;
/* Create Hash Section */
TargetFile,readErr := os.Open(FileToHash);
check(readErr," Error opening FileToHash.");
defer TargetFile.Close();
switch WhichHash { // Initialing case switch on WhichHash
case md5hash :
hasher = md5.New();
case sha1hash :
hasher = sha1.New();
case sha256hash :
hasher = sha256.New();
case sha384hash :
hasher = sha512.New384();
case sha512hash :
hasher = sha512.New();
default:
hasher = sha256.New();
} /* initializing case on WhichHash */
FileReadBuffer := make([]byte,ReadBufferSize);
for { // Repeat Until eof loop.
n,err := TargetFile.Read(FileReadBuffer);
if n == 0 || err == io.EOF { break }
check(err," Unexpected error while reading the target file on which
to compute the hash,");
hasher.Write(FileReadBuffer);
FileSize += int64(n);
} // Repeat Until TargetFile.eof loop;
HashValueComputedStr := hex.EncodeToString(hasher.Sum(nil));
fmt.Println(" Filename = ",FileToHash,", FileSize = ",FileSize,",
",HashName[WhichHash]," computed hash string, followed by hash string in
the file are : ");
fmt.Println(" Computed hash hex encoded:",HashValueComputedStr);
TargetFile.Close(); // Close the handle to allow opening a target
from the next line, if there is one.
fmt.Println();
fmt.Println();
WhichHash++
if WhichHash > sha512hash {break}
} /* outer LOOP */
fmt.Println();
}
// --- check
---
func check(e error, msg string) {
if e != nil {
fmt.Errorf("%s : ",msg);
panic(e);
}
}
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.