-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sbom): add checksum to files (#3888)
Co-authored-by: knqyf263 <[email protected]>
- Loading branch information
1 parent
00de24b
commit 67236f6
Showing
37 changed files
with
640 additions
and
371 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"SPDXID": "SPDXRef-DOCUMENT", | ||
"creationInfo": { | ||
"created": "2023-01-08T23:58:16.700785648Z", | ||
"created": "2023-03-29T19:07:18Z", | ||
"creators": [ | ||
"Tool: trivy-dev", | ||
"Organization: aquasecurity" | ||
|
@@ -11,14 +11,26 @@ | |
"documentDescribes": [ | ||
"SPDXRef-Filesystem-6e0ac6a0fab50ab4" | ||
], | ||
"documentNamespace": "http://aquasecurity.github.io/trivy/filesystem/testdata/fixtures/fs/conda-3be0d21e-5711-451e-8b1b-2ac8775a3abb", | ||
"documentNamespace": "http://aquasecurity.github.io/trivy/filesystem/testdata/fixtures/fs/conda-d872c7e3-4c6c-4fa1-a9b6-3e69dc71ff3b", | ||
"files": [ | ||
{ | ||
"SPDXID": "SPDXRef-File-600e5e0110a84891", | ||
"checksums": [ | ||
{ | ||
"algorithm": "SHA1", | ||
"checksumValue": "237db0da53131e4548cb1181337fa0f420299e1f" | ||
} | ||
], | ||
"fileName": "miniconda3/envs/testenv/conda-meta/openssl-1.1.1q-h7f8727e_0.json" | ||
}, | ||
{ | ||
"SPDXID": "SPDXRef-File-7eb62e2a3edddc0a", | ||
"checksums": [ | ||
{ | ||
"algorithm": "SHA1", | ||
"checksumValue": "a6a2db7668f1ad541d704369fc66c96a4415aa24" | ||
} | ||
], | ||
"fileName": "miniconda3/envs/testenv/conda-meta/pip-22.2.2-py38h06a4308_0.json" | ||
} | ||
], | ||
|
@@ -33,50 +45,50 @@ | |
}, | ||
{ | ||
"SPDXID": "SPDXRef-Filesystem-6e0ac6a0fab50ab4", | ||
"downloadLocation": "NONE", | ||
"attributionTexts": [ | ||
"SchemaVersion: 2" | ||
], | ||
"downloadLocation": "NONE", | ||
"filesAnalyzed": false, | ||
"name": "testdata/fixtures/fs/conda" | ||
}, | ||
{ | ||
"SPDXID": "SPDXRef-Package-2984084f02572600", | ||
"SPDXID": "SPDXRef-Package-6b677e82217fb5bd", | ||
"downloadLocation": "NONE", | ||
"externalRefs": [ | ||
{ | ||
"referenceCategory": "PACKAGE-MANAGER", | ||
"referenceLocator": "pkg:conda/[email protected]", | ||
"referenceLocator": "pkg:conda/[email protected]", | ||
"referenceType": "purl" | ||
} | ||
], | ||
"filesAnalyzed": false, | ||
"hasFiles": [ | ||
"SPDXRef-File-600e5e0110a84891" | ||
"SPDXRef-File-7eb62e2a3edddc0a" | ||
], | ||
"licenseConcluded": "OpenSSL", | ||
"licenseDeclared": "OpenSSL", | ||
"name": "openssl", | ||
"versionInfo": "1.1.1q" | ||
"licenseConcluded": "MIT", | ||
"licenseDeclared": "MIT", | ||
"name": "pip", | ||
"versionInfo": "22.2.2" | ||
}, | ||
{ | ||
"SPDXID": "SPDXRef-Package-ac33eb699b3aa81d", | ||
"SPDXID": "SPDXRef-Package-b1088cb4090e3a55", | ||
"downloadLocation": "NONE", | ||
"externalRefs": [ | ||
{ | ||
"referenceCategory": "PACKAGE-MANAGER", | ||
"referenceLocator": "pkg:conda/[email protected]", | ||
"referenceLocator": "pkg:conda/[email protected]", | ||
"referenceType": "purl" | ||
} | ||
], | ||
"filesAnalyzed": false, | ||
"hasFiles": [ | ||
"SPDXRef-File-7eb62e2a3edddc0a" | ||
"SPDXRef-File-600e5e0110a84891" | ||
], | ||
"licenseConcluded": "MIT", | ||
"licenseDeclared": "MIT", | ||
"name": "pip", | ||
"versionInfo": "22.2.2" | ||
"licenseConcluded": "OpenSSL", | ||
"licenseDeclared": "OpenSSL", | ||
"name": "openssl", | ||
"versionInfo": "1.1.1q" | ||
} | ||
], | ||
"relationships": [ | ||
|
@@ -91,12 +103,12 @@ | |
"spdxElementId": "SPDXRef-Filesystem-6e0ac6a0fab50ab4" | ||
}, | ||
{ | ||
"relatedSpdxElement": "SPDXRef-Package-2984084f02572600", | ||
"relatedSpdxElement": "SPDXRef-Package-b1088cb4090e3a55", | ||
"relationshipType": "CONTAINS", | ||
"spdxElementId": "SPDXRef-Application-ee5ef1aa4ac89125" | ||
}, | ||
{ | ||
"relatedSpdxElement": "SPDXRef-Package-ac33eb699b3aa81d", | ||
"relatedSpdxElement": "SPDXRef-Package-6b677e82217fb5bd", | ||
"relationshipType": "CONTAINS", | ||
"spdxElementId": "SPDXRef-Application-ee5ef1aa4ac89125" | ||
} | ||
|
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,78 @@ | ||
package digest | ||
|
||
import ( | ||
"crypto/sha1" // nolint | ||
"crypto/sha256" | ||
"fmt" | ||
"hash" | ||
"io" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
type Algorithm string | ||
|
||
func (a Algorithm) String() string { | ||
return string(a) | ||
} | ||
|
||
// supported digest types | ||
const ( | ||
SHA1 Algorithm = "sha1" // sha1 with hex encoding (lower case only) | ||
SHA256 Algorithm = "sha256" // sha256 with hex encoding (lower case only) | ||
) | ||
|
||
// Digest allows simple protection of hex formatted digest strings, prefixed by their algorithm. | ||
// | ||
// The following is an example of the contents of Digest types: | ||
// | ||
// sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc | ||
type Digest string | ||
|
||
// NewDigest returns a Digest from alg and a hash.Hash object. | ||
func NewDigest(alg Algorithm, h hash.Hash) Digest { | ||
return Digest(fmt.Sprintf("%s:%x", alg, h.Sum(nil))) | ||
} | ||
|
||
func (d Digest) Algorithm() Algorithm { | ||
return Algorithm(d[:d.sepIndex()]) | ||
} | ||
|
||
func (d Digest) Encoded() string { | ||
return string(d[d.sepIndex()+1:]) | ||
} | ||
|
||
func (d Digest) String() string { | ||
return string(d) | ||
} | ||
|
||
func (d Digest) sepIndex() int { | ||
i := strings.Index(string(d), ":") | ||
if i < 0 { | ||
i = 0 | ||
} | ||
return i | ||
} | ||
|
||
func CalcSHA1(r io.ReadSeeker) (Digest, error) { | ||
defer r.Seek(0, io.SeekStart) | ||
|
||
h := sha1.New() // nolint | ||
if _, err := io.Copy(h, r); err != nil { | ||
return "", xerrors.Errorf("unable to calculate sha1 digest: %w", err) | ||
} | ||
|
||
return NewDigest(SHA1, h), nil | ||
} | ||
|
||
func CalcSHA256(r io.ReadSeeker) (Digest, error) { | ||
defer r.Seek(0, io.SeekStart) | ||
|
||
h := sha256.New() | ||
if _, err := io.Copy(h, r); err != nil { | ||
return "", xerrors.Errorf("unable to calculate sha256 digest: %w", err) | ||
} | ||
|
||
return NewDigest(SHA256, h), nil | ||
} |
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
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
Oops, something went wrong.