Skip to content

Commit

Permalink
feat(builders): add dependency hashing for ant
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhao committed Jun 1, 2018
1 parent b908880 commit bd0af6a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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
42 changes: 42 additions & 0 deletions builders/builderutil/hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package builderutil

import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"io"
"os"

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

// GetHashes computes 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 = string(sha1Hash.Sum(nil))

md5Hash := md5.New()
if _, err := io.Copy(md5Hash, f); err != nil {
return hashes, err
}
hashes.MD5 = string(md5Hash.Sum(nil))

sha256Hash := sha256.New()
if _, err := io.Copy(sha256Hash, f); err != nil {
return hashes, err
}
hashes.SHA256 = string(sha256Hash.Sum(nil))

return hashes, err
}

0 comments on commit bd0af6a

Please sign in to comment.