Skip to content

Commit

Permalink
fix(builders): fix hash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhao committed Jun 15, 2018
1 parent 607de55 commit 1239291
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions builders/builderutil/hashes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builderutil

import (
"bufio"
"crypto/md5" // nolint: gas
"crypto/sha1"
"crypto/sha256"
Expand All @@ -21,23 +22,22 @@ func GetHashes(path string) (module.Hashes, error) {
}
defer f.Close()

sha1Hash := sha1.New()
if _, err := io.Copy(sha1Hash, f); err != nil {
return hashes, err
}
hashes.SHA1 = hex.EncodeToString(sha1Hash.Sum(nil))

md5Hash := md5.New() // nolint: gas
if _, err := io.Copy(md5Hash, f); err != nil {
return hashes, err
md5 := md5.New() // nolint: gas
sha1 := sha1.New()
sha256 := sha256.New()

// Read the file once and write to a multiplexer
// so we can calculate all hashes simultaneously
pagesize := os.Getpagesize()
reader := bufio.NewReaderSize(f, pagesize)
multiWriter := io.MultiWriter(md5, sha1, sha256)
if _, err := io.Copy(multiWriter, reader); err != nil {
panic(err.Error())
}
hashes.MD5 = hex.EncodeToString(md5Hash.Sum(nil))

sha256Hash := sha256.New()
if _, err := io.Copy(sha256Hash, f); err != nil {
return hashes, err
}
hashes.SHA256 = hex.EncodeToString(sha256Hash.Sum(nil))
hashes.MD5 = hex.EncodeToString(md5.Sum(nil))
hashes.SHA1 = hex.EncodeToString(sha1.Sum(nil))
hashes.SHA256 = hex.EncodeToString(sha256.Sum(nil))

return hashes, err
}

0 comments on commit 1239291

Please sign in to comment.