Skip to content

Commit 12d6706

Browse files
authored
feat(sbom): add SHA-512 hash support for CycloneDX SBOM (aquasecurity#9126)
1 parent 42ccd3d commit 12d6706

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

pkg/digest/digest.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func (a Algorithm) String() string {
2121
const (
2222
SHA1 Algorithm = "sha1" // sha1 with hex encoding (lower case only)
2323
SHA256 Algorithm = "sha256" // sha256 with hex encoding (lower case only)
24-
MD5 Algorithm = "md5" // md5 with hex encoding (lower case only)
24+
SHA512 Algorithm = "sha512"
25+
MD5 Algorithm = "md5" // md5 with hex encoding (lower case only)
2526
)
2627

2728
// Digest allows simple protection of hex formatted digest strings, prefixed by their algorithm.

pkg/sbom/cyclonedx/marshal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ func (m *Marshaler) Hashes(files []core.File) *[]cdx.Hash {
273273
alg = cdx.HashAlgoSHA1
274274
case digest.SHA256:
275275
alg = cdx.HashAlgoSHA256
276+
case digest.SHA512:
277+
alg = cdx.HashAlgoSHA512
276278
case digest.MD5:
277279
alg = cdx.HashAlgoMD5
278280
default:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
3+
"bomFormat": "CycloneDX",
4+
"specVersion": "1.5",
5+
"serialNumber": "urn:uuid:379ddfdb-306b-44e4-bff3-9bfb9bbc5fa5",
6+
"version": 1,
7+
"metadata": {
8+
"timestamp": "2022-05-28T10:20:03.79527Z",
9+
"tools": [
10+
{
11+
"vendor": "aquasecurity",
12+
"name": "trivy",
13+
"version": "dev"
14+
}
15+
],
16+
"component": {
17+
"bom-ref": "0f585d64-4815-4b72-92c5-97dae191fa4a",
18+
"type": "container",
19+
"name": "maven-test-project"
20+
}
21+
},
22+
"components": [
23+
{
24+
"bom-ref": "@angular/animations@19.2.10",
25+
"type": "library",
26+
"name": "@angular/animations",
27+
"version": "19.2.10",
28+
"scope": "required",
29+
"author": "angular",
30+
"description": "Angular - animations integration with web-animations",
31+
"purl": "pkg:npm/%40angular/animations@19.2.10",
32+
"hashes": [
33+
{
34+
"alg": "SHA-512",
35+
"content": "2e51fa9add03f3e308d0b57c40dc7dfeba8b2efd1609f60f4bfe625d21a92327ec7e52e83b97511a1b52e297506eee60aa69cb75ff62eebe257512637fbc1bfa"
36+
}
37+
]
38+
}
39+
]
40+
}

pkg/sbom/cyclonedx/unmarshal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ func (b *BOM) unmarshalHashes(hashes *[]cdx.Hash) []digest.Digest {
257257
alg = digest.SHA1
258258
case cdx.HashAlgoSHA256:
259259
alg = digest.SHA256
260+
case cdx.HashAlgoSHA512:
261+
alg = digest.SHA512
260262
case cdx.HashAlgoMD5:
261263
alg = digest.MD5
262264
default:

pkg/sbom/cyclonedx/unmarshal_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,34 @@ func TestUnmarshaler_Unmarshal(t *testing.T) {
848848
},
849849
},
850850
},
851+
{
852+
name: "SHA-512",
853+
inputFile: "testdata/happy/package-hashes.json",
854+
want: types.SBOM{
855+
Applications: []ftypes.Application{
856+
{
857+
Type: ftypes.NodePkg,
858+
Packages: ftypes.Packages{
859+
{
860+
ID: "@angular/animations@19.2.10",
861+
Name: "@angular/animations",
862+
Version: "19.2.10",
863+
Identifier: ftypes.PkgIdentifier{
864+
PURL: &packageurl.PackageURL{
865+
Type: packageurl.TypeNPM,
866+
Namespace: "@angular",
867+
Name: "animations",
868+
Version: "19.2.10",
869+
},
870+
BOMRef: "@angular/animations@19.2.10",
871+
},
872+
Digest: "sha512:2e51fa9add03f3e308d0b57c40dc7dfeba8b2efd1609f60f4bfe625d21a92327ec7e52e83b97511a1b52e297506eee60aa69cb75ff62eebe257512637fbc1bfa",
873+
},
874+
},
875+
},
876+
},
877+
},
878+
},
851879
{
852880
name: "invalid serial",
853881
inputFile: "testdata/sad/invalid-serial.json",

0 commit comments

Comments
 (0)