github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/helpers/sha.go (about)

     1  package helpers
     2  
     3  import (
     4  	"crypto/sha1"
     5  	"crypto/sha256"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  // Sha1Sum calculates the SHA1 sum of a file.
    15  func Sha1Sum(path string) string {
    16  	f, err := os.Open(path)
    17  	Expect(err).ToNot(HaveOccurred())
    18  	defer f.Close()
    19  
    20  	hash := sha1.New()
    21  	_, err = io.Copy(hash, f)
    22  	Expect(err).ToNot(HaveOccurred())
    23  
    24  	return fmt.Sprintf("%x", hash.Sum(nil))
    25  }
    26  
    27  // Sha256Sum calculates the SHA256 sum of a file.
    28  func Sha256Sum(path string) string {
    29  	f, err := os.Open(path)
    30  	Expect(err).ToNot(HaveOccurred())
    31  	defer f.Close()
    32  
    33  	hash := sha256.New()
    34  	_, err = io.Copy(hash, f)
    35  	Expect(err).ToNot(HaveOccurred())
    36  
    37  	return fmt.Sprintf("%x", hash.Sum(nil))
    38  }
    39  
    40  // Sha256SumDirectory calculates the SHA256 sum of a directory.
    41  func Sha256SumDirectory(dir string) string {
    42  	tempZipFile, err := ioutil.TempFile(os.TempDir(), "")
    43  	Expect(err).ToNot(HaveOccurred())
    44  
    45  	err = Zipit(dir, tempZipFile.Name(), "")
    46  	Expect(err).ToNot(HaveOccurred())
    47  
    48  	return Sha256Sum(tempZipFile.Name())
    49  }