Skip to content

Commit

Permalink
Refactoring: use crypto.Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
moio committed Feb 10, 2018
1 parent cdac963 commit 5ff7a34
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
9 changes: 5 additions & 4 deletions get/filestorage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package get

import (
"crypto"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
Expand All @@ -25,7 +26,7 @@ func NewFileStorage(directory string) Storage {
}

// Checksum returns the checksum value of a file in the permanent location, according to the checksumType algorithm
func (s *FileStorage) Checksum(filename string, checksumType ChecksumType) (checksum string, err error) {
func (s *FileStorage) Checksum(filename string, hash crypto.Hash) (checksum string, err error) {
fullPath := path.Join(s.directory, filename)
stat, err := os.Stat(fullPath)
if os.IsNotExist(err) || stat == nil {
Expand All @@ -39,14 +40,14 @@ func (s *FileStorage) Checksum(filename string, checksumType ChecksumType) (chec
}
defer f.Close()

switch checksumType {
case SHA1:
switch hash {
case crypto.SHA1:
h := sha1.New()
if _, err = io.CopyBuffer(h, f, s.checksumBuffer); err != nil {
log.Fatal(err)
}
checksum = hex.EncodeToString(h.Sum(nil))
case SHA256:
case crypto.SHA256:
h := sha256.New()
if _, err = io.CopyBuffer(h, f, s.checksumBuffer); err != nil {
log.Fatal(err)
Expand Down
3 changes: 2 additions & 1 deletion get/s3storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package get

import (
"crypto"
"errors"
"io"
"log"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (s *S3Storage) newPrefix() string {
}

// Checksum returns the checksum value of a file in the permanent location, according to the checksumType algorithm
func (s *S3Storage) Checksum(filename string, checksumType ChecksumType) (checksum string, err error) {
func (s *S3Storage) Checksum(filename string, hash crypto.Hash) (checksum string, err error) {
input := &s3.HeadObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(s.prefix + filename),
Expand Down
13 changes: 2 additions & 11 deletions get/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package get

import (
"crypto"
"errors"

"github.com/moio/minima/util"
Expand All @@ -16,20 +17,10 @@ type Storage interface {
Commit() (err error)
// Checksum returns the checksum value of a file in the permanent location, according to the checksumType algorithm
// returns ErrFileNotFound if the requested path was not found at all
Checksum(filename string, checksumType ChecksumType) (checksum string, err error)
Checksum(filename string, hash crypto.Hash) (checksum string, err error)
// Recycle will copy a file from the permanent to the temporary location
Recycle(filename string) (err error)
}

// ChecksumType is an enumeration of supported checksum algorithms
type ChecksumType int

const (
// SHA1 identifier
SHA1 ChecksumType = iota
// SHA256 identifier
SHA256
)

// ErrFileNotFound signals that the requested file was not found
var ErrFileNotFound = errors.New("File not found")
11 changes: 6 additions & 5 deletions get/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package get

import (
"compress/gzip"
"crypto"
"encoding/xml"
"io"
"log"
Expand Down Expand Up @@ -49,10 +50,10 @@ type XMLChecksum struct {
Checksum string `xml:",cdata"`
}

var checksumTypeMap = map[string]ChecksumType{
"sha": SHA1,
"sha1": SHA1,
"sha256": SHA256,
var hashMap = map[string]crypto.Hash{
"sha": crypto.SHA1,
"sha1": crypto.SHA1,
"sha256": crypto.SHA256,
}

const repomdPath = "repodata/repomd.xml"
Expand Down Expand Up @@ -211,7 +212,7 @@ func (r *Syncer) processPrimary(path string) (packagesToDownload []XMLPackage, p
allArchs := len(r.archs) == 0
for _, pack := range primary.Packages {
if allArchs || pack.Arch == "noarch" || r.archs[pack.Arch] {
storageChecksum, err := r.storage.Checksum(pack.Location.Href, checksumTypeMap[pack.Checksum.Type])
storageChecksum, err := r.storage.Checksum(pack.Location.Href, hashMap[pack.Checksum.Type])
switch {
case err == ErrFileNotFound:
log.Printf("...package '%v' not found, will be downloaded\n", pack.Location.Href)
Expand Down

0 comments on commit 5ff7a34

Please sign in to comment.