Skip to content

Commit c86fa51

Browse files
authored
Merge pull request #143 from fossas/feat/compute-dependency-hashes
Feat/compute dependency hashes
2 parents 9049c67 + e4a5aec commit c86fa51

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

builders/ant/ant.go

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/bmatcuk/doublestar"
1515
"github.com/gnewton/jargo"
1616

17+
"github.com/fossas/fossa-cli/builders/builderutil"
1718
"github.com/fossas/fossa-cli/builders/maven"
1819
"github.com/fossas/fossa-cli/exec"
1920
"github.com/fossas/fossa-cli/files"
@@ -94,8 +95,10 @@ func (builder *AntBuilder) Analyze(m module.Module, allowUnresolved bool) ([]mod
9495
for _, jarFilePath := range jarFilePaths {
9596
locator, err := locatorFromJar(jarFilePath)
9697
if err == nil {
98+
hashes, _ := builderutil.GetHashes(jarFilePath)
9799
dependencies = append(dependencies, module.Dependency{
98100
Locator: locator,
101+
Hashes: hashes,
99102
})
100103
} else {
101104
log.Logger.Warningf("unable to resolve Jar: %s", jarFilePath)

builders/builderutil/hashes.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package builderutil
2+
3+
import (
4+
"crypto/md5" // nolint: gas
5+
"crypto/sha1"
6+
"crypto/sha256"
7+
"encoding/hex"
8+
"io"
9+
"os"
10+
11+
"github.com/fossas/fossa-cli/module"
12+
)
13+
14+
// GetHashes computes hexadecimal checksums of a variety of types for a given file path
15+
func GetHashes(path string) (module.Hashes, error) {
16+
hashes := module.Hashes{}
17+
18+
f, err := os.Open(path)
19+
if err != nil {
20+
return hashes, err
21+
}
22+
defer f.Close()
23+
24+
sha1Hash := sha1.New()
25+
if _, err := io.Copy(sha1Hash, f); err != nil {
26+
return hashes, err
27+
}
28+
hashes.SHA1 = hex.EncodeToString(sha1Hash.Sum(nil))
29+
30+
md5Hash := md5.New() // nolint: gas
31+
if _, err := io.Copy(md5Hash, f); err != nil {
32+
return hashes, err
33+
}
34+
hashes.MD5 = hex.EncodeToString(md5Hash.Sum(nil))
35+
36+
sha256Hash := sha256.New()
37+
if _, err := io.Copy(sha256Hash, f); err != nil {
38+
return hashes, err
39+
}
40+
hashes.SHA256 = hex.EncodeToString(sha256Hash.Sum(nil))
41+
42+
return hashes, err
43+
}

module/dependency.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package module
33
// Dependency represents a code library brought in by running a Build
44
type Dependency struct {
55
Locator
6-
6+
Hashes
77
Via []ImportPath
88
}

module/hashes.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package module
2+
3+
// Hashes contains hexadecimal checksums of code libraries brought in by running a Build
4+
type Hashes struct {
5+
SHA1 string
6+
SHA256 string
7+
MD5 string
8+
}

0 commit comments

Comments
 (0)