Skip to content

Commit

Permalink
Merge pull request #143 from fossas/feat/compute-dependency-hashes
Browse files Browse the repository at this point in the history
Feat/compute dependency hashes
  • Loading branch information
xizhao committed Jun 7, 2018
2 parents 9049c67 + e4a5aec commit c86fa51
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions builders/ant/ant.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bmatcuk/doublestar"
"github.com/gnewton/jargo"

"github.com/fossas/fossa-cli/builders/builderutil"
"github.com/fossas/fossa-cli/builders/maven"
"github.com/fossas/fossa-cli/exec"
"github.com/fossas/fossa-cli/files"
Expand Down Expand Up @@ -94,8 +95,10 @@ func (builder *AntBuilder) Analyze(m module.Module, allowUnresolved bool) ([]mod
for _, jarFilePath := range jarFilePaths {
locator, err := locatorFromJar(jarFilePath)
if err == nil {
hashes, _ := builderutil.GetHashes(jarFilePath)
dependencies = append(dependencies, module.Dependency{
Locator: locator,
Hashes: hashes,
})
} else {
log.Logger.Warningf("unable to resolve Jar: %s", jarFilePath)
Expand Down
43 changes: 43 additions & 0 deletions builders/builderutil/hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package builderutil

import (
"crypto/md5" // nolint: gas
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"io"
"os"

"github.com/fossas/fossa-cli/module"
)

// GetHashes computes hexadecimal checksums of a variety of types for a given file path
func GetHashes(path string) (module.Hashes, error) {
hashes := module.Hashes{}

f, err := os.Open(path)
if err != nil {
return hashes, err
}
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
}
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))

return hashes, err
}
2 changes: 1 addition & 1 deletion module/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package module
// Dependency represents a code library brought in by running a Build
type Dependency struct {
Locator

Hashes
Via []ImportPath
}
8 changes: 8 additions & 0 deletions module/hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package module

// Hashes contains hexadecimal checksums of code libraries brought in by running a Build
type Hashes struct {
SHA1 string
SHA256 string
MD5 string
}

0 comments on commit c86fa51

Please sign in to comment.