5
5
6
6
namespace CycloneDX . CLI
7
7
{
8
+ public enum SpdxVersion
9
+ {
10
+ v2_1 ,
11
+ v2_2
12
+ }
13
+
8
14
public static class SpdxTagSerializer
9
15
{
10
16
public class SpdxSerializationException : Exception
11
17
{
12
18
public SpdxSerializationException ( string message ) : base ( message ) { }
13
19
}
14
20
15
- public static string Serialize ( CycloneDX . Models . v1_2 . Bom bom )
21
+ public static string Serialize ( CycloneDX . Models . v1_2 . Bom bom , SpdxVersion version )
16
22
{
17
23
if ( bom . Metadata ? . Component ? . Name == null || bom . Metadata ? . Component ? . Version == null )
18
24
throw new SpdxSerializationException ( "For SPDX output top level component name and version are required in the BOM metadata" ) ;
@@ -34,7 +40,11 @@ public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
34
40
35
41
var sb = new StringBuilder ( ) ;
36
42
var componentSb = new StringBuilder ( ) ;
37
- sb . AppendLine ( "SPDXVersion: SPDX-2.1" ) ;
43
+ sb . Append ( "SPDXVersion: SPDX-" ) ;
44
+ if ( version == SpdxVersion . v2_1 )
45
+ sb . Append ( "2.1" ) ;
46
+ else if ( version == SpdxVersion . v2_2 )
47
+ sb . Append ( "2.2" ) ;
38
48
// CC0-1.0 is a requirement when using the SPDX specification
39
49
sb . AppendLine ( "DataLicense: CC0-1.0" ) ;
40
50
sb . AppendLine ( $ "SPDXID: SPDXRef-DOCUMENT") ;
@@ -81,6 +91,7 @@ public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
81
91
foreach ( var hash in component . Hashes )
82
92
{
83
93
string algStr = null ;
94
+
84
95
switch ( hash . Alg )
85
96
{
86
97
case CycloneDX . Models . v1_2 . Hash . HashAlgorithm . SHA_1 :
@@ -89,14 +100,18 @@ public static string Serialize(CycloneDX.Models.v1_2.Bom bom)
89
100
case CycloneDX . Models . v1_2 . Hash . HashAlgorithm . SHA_256 :
90
101
algStr = "SHA256" ;
91
102
break ;
92
- // following algorithms only supported in v2.2
93
- // case Hash.HashAlgorithm.SHA_384:
94
- // algStr = "SHA384";
95
- // break;
96
- // case Hash.HashAlgorithm.SHA_512:
97
- // algStr = "SHA512";
98
- // break;
99
103
}
104
+ if ( version == SpdxVersion . v2_2 )
105
+ switch ( hash . Alg )
106
+ {
107
+ case CycloneDX . Models . v1_2 . Hash . HashAlgorithm . SHA_384 :
108
+ algStr = "SHA384" ;
109
+ break ;
110
+ case CycloneDX . Models . v1_2 . Hash . HashAlgorithm . SHA_512 :
111
+ algStr = "SHA512" ;
112
+ break ;
113
+ }
114
+
100
115
if ( algStr != null )
101
116
{
102
117
sb . AppendLine ( $ "PackageChecksum: { algStr } : { hash . Content } ") ;
0 commit comments