Skip to content

Commit d5df6c2

Browse files
authored
Merge pull request #603 from justahero/sebastian/implement-multiple-signature
Implement multiple variants of Signature field
2 parents 3f0bedd + 5ad8bcb commit d5df6c2

File tree

6 files changed

+416
-100
lines changed

6 files changed

+416
-100
lines changed

cyclonedx-bom/src/models/component.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,7 @@ mod test {
641641
))])),
642642
copyright: Some(CopyrightTexts(vec![Copyright("copyright".to_string())])),
643643
}),
644-
signature: Some(Signature {
645-
algorithm: Algorithm::HS512,
646-
value: "abcdefgh".to_string(),
647-
}),
644+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
648645
}])
649646
.validate();
650647

@@ -732,10 +729,7 @@ mod test {
732729
))])),
733730
copyright: Some(CopyrightTexts(vec![Copyright("copyright".to_string())])),
734731
}),
735-
signature: Some(Signature {
736-
algorithm: Algorithm::HS512,
737-
value: "abcdefgh".to_string(),
738-
}),
732+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
739733
}])
740734
.validate();
741735

cyclonedx-bom/src/models/composition.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ mod test {
134134
aggregate: AggregateType::Complete,
135135
assemblies: Some(vec![BomReference("reference".to_string())]),
136136
dependencies: Some(vec![BomReference("reference".to_string())]),
137-
signature: Some(Signature {
138-
algorithm: Algorithm::HS512,
139-
value: "abcdefgh".to_string(),
140-
}),
137+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
141138
}])
142139
.validate();
143140

@@ -150,10 +147,7 @@ mod test {
150147
aggregate: AggregateType::UnknownAggregateType("unknown aggregate type".to_string()),
151148
assemblies: Some(vec![BomReference("reference".to_string())]),
152149
dependencies: Some(vec![BomReference("reference".to_string())]),
153-
signature: Some(Signature {
154-
algorithm: Algorithm::HS512,
155-
value: "abcdefgh".to_string(),
156-
}),
150+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
157151
}])
158152
.validate();
159153

cyclonedx-bom/src/models/service.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,7 @@ mod test {
315315
value: NormalizedString::new("value"),
316316
}])),
317317
services: Some(Services(vec![])),
318-
signature: Some(Signature {
319-
algorithm: Algorithm::HS512,
320-
value: "abcdefgh".to_string(),
321-
}),
318+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
322319
}])
323320
.validate();
324321

@@ -377,10 +374,7 @@ mod test {
377374
services: None,
378375
signature: None,
379376
}])),
380-
signature: Some(Signature {
381-
algorithm: Algorithm::HS512,
382-
value: "abcdefgh".to_string(),
383-
}),
377+
signature: Some(Signature::single(Algorithm::HS512, "abcdefgh")),
384378
}])
385379
.validate();
386380

cyclonedx-bom/src/models/signature.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,15 @@
1818

1919
use std::str::FromStr;
2020

21-
/// Enveloped signature in [JSON Signature Format (JSF)](https://cyberphone.github.io/doc/security/jsf.html)
22-
#[derive(Clone, Debug, PartialEq, Eq)]
23-
pub struct Signature {
24-
/// Signature algorithm.
25-
pub algorithm: Algorithm,
26-
/// The signature data.
27-
pub value: String,
28-
}
29-
30-
/*
3121
/// Enveloped signature in [JSON Signature Format (JSF)](https://cyberphone.github.io/doc/security/jsf.html)
3222
#[derive(Clone, Debug, PartialEq, Eq)]
3323
pub enum Signature {
3424
/// Multiple signatures
3525
Signers(Vec<Signer>),
36-
/// A single signature chain
37-
Chain(Signer),
26+
/// A signature chain consisting of multiple signatures
27+
Chain(Vec<Signer>),
3828
/// A single signature
39-
Signature(Signer),
29+
Single(Signer),
4030
}
4131

4232
/// For now the [`Signer`] struct only holds algorithm and value
@@ -47,10 +37,48 @@ pub struct Signer {
4737
/// The signature data.
4838
pub value: String,
4939
}
50-
*/
40+
41+
impl Signer {
42+
pub fn new(algorithm: Algorithm, value: &str) -> Self {
43+
Self {
44+
algorithm,
45+
value: value.to_string(),
46+
}
47+
}
48+
}
49+
50+
impl Signature {
51+
/// Creates a single signature.
52+
pub fn single(algorithm: Algorithm, value: &str) -> Self {
53+
Self::Single(Signer {
54+
algorithm,
55+
value: value.to_string(),
56+
})
57+
}
58+
59+
/// Creates a chain of multiple signatures
60+
pub fn chain(chain: &[(Algorithm, &str)]) -> Self {
61+
Self::Chain(
62+
chain
63+
.iter()
64+
.map(|(algorithm, value)| Signer::new(*algorithm, value))
65+
.collect(),
66+
)
67+
}
68+
69+
/// Creates a list of multiple signatures.
70+
pub fn signers(signers: &[(Algorithm, &str)]) -> Self {
71+
Self::Signers(
72+
signers
73+
.iter()
74+
.map(|(algorithm, value)| Signer::new(*algorithm, value))
75+
.collect(),
76+
)
77+
}
78+
}
5179

5280
/// Supported signature algorithms.
53-
#[derive(Clone, Debug, PartialEq, Eq)]
81+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5482
pub enum Algorithm {
5583
RS256,
5684
RS384,

0 commit comments

Comments
 (0)