-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from fossas/feat/compute-dependency-hashes
Feat/compute dependency hashes
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |