22
33use :: signature:: SignatureEncoding ;
44use alloc:: boxed:: Box ;
5- use core:: fmt:: { Debug , Display , Formatter , LowerHex , UpperHex } ;
5+ use core:: {
6+ fmt:: { self , Debug , Display , Formatter , LowerHex , UpperHex } ,
7+ marker:: PhantomData ,
8+ } ;
69use crypto_bigint:: BoxedUint ;
710
11+ use digest:: Digest ;
812#[ cfg( feature = "serde" ) ]
913use serdect:: serde:: { de, Deserialize , Serialize } ;
14+ use signature:: PrehashSignature ;
1015use spki:: {
1116 der:: { asn1:: BitString , Result as DerResult } ,
1217 SignatureBitStringEncoding ,
@@ -15,22 +20,46 @@ use spki::{
1520/// `RSASSA-PKCS1-v1_5` signatures as described in [RFC8017 § 8.2].
1621///
1722/// [RFC8017 § 8.2]: https://datatracker.ietf.org/doc/html/rfc8017#section-8.2
18- #[ derive( Debug , Clone , PartialEq , Eq ) ]
19- pub struct Signature {
23+ #[ derive( Eq ) ]
24+ pub struct Signature < D > {
2025 pub ( super ) inner : BoxedUint ,
26+ _digest : PhantomData < D > ,
27+ }
28+
29+ impl < D > Debug for Signature < D > {
30+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
31+ f. debug_struct ( "Signature" )
32+ . field ( "inner" , & self . inner )
33+ . finish ( )
34+ }
2135}
2236
23- impl SignatureEncoding for Signature {
37+ impl < D > Clone for Signature < D > {
38+ fn clone ( & self ) -> Self {
39+ Self {
40+ inner : self . inner . clone ( ) ,
41+ _digest : PhantomData ,
42+ }
43+ }
44+ }
45+
46+ impl < D > PartialEq for Signature < D > {
47+ fn eq ( & self , other : & Self ) -> bool {
48+ self . inner . eq ( & other. inner )
49+ }
50+ }
51+
52+ impl < D > SignatureEncoding for Signature < D > {
2453 type Repr = Box < [ u8 ] > ;
2554}
2655
27- impl SignatureBitStringEncoding for Signature {
56+ impl < D > SignatureBitStringEncoding for Signature < D > {
2857 fn to_bitstring ( & self ) -> DerResult < BitString > {
2958 BitString :: new ( 0 , self . to_vec ( ) )
3059 }
3160}
3261
33- impl TryFrom < & [ u8 ] > for Signature {
62+ impl < D > TryFrom < & [ u8 ] > for Signature < D > {
3463 type Error = signature:: Error ;
3564
3665 fn try_from ( bytes : & [ u8 ] ) -> signature:: Result < Self > {
@@ -42,17 +71,20 @@ impl TryFrom<&[u8]> for Signature {
4271 #[ cfg( not( feature = "std" ) ) ]
4372 let inner = inner. map_err ( |_| signature:: Error :: new ( ) ) ?;
4473
45- Ok ( Self { inner } )
74+ Ok ( Self {
75+ inner,
76+ _digest : PhantomData ,
77+ } )
4678 }
4779}
4880
49- impl From < Signature > for Box < [ u8 ] > {
50- fn from ( signature : Signature ) -> Box < [ u8 ] > {
81+ impl < D > From < Signature < D > > for Box < [ u8 ] > {
82+ fn from ( signature : Signature < D > ) -> Box < [ u8 ] > {
5183 signature. inner . to_be_bytes ( )
5284 }
5385}
5486
55- impl LowerHex for Signature {
87+ impl < D > LowerHex for Signature < D > {
5688 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
5789 for byte in self . to_bytes ( ) . iter ( ) {
5890 write ! ( f, "{:02x}" , byte) ?;
@@ -61,7 +93,7 @@ impl LowerHex for Signature {
6193 }
6294}
6395
64- impl UpperHex for Signature {
96+ impl < D > UpperHex for Signature < D > {
6597 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
6698 for byte in self . to_bytes ( ) . iter ( ) {
6799 write ! ( f, "{:02X}" , byte) ?;
@@ -70,14 +102,14 @@ impl UpperHex for Signature {
70102 }
71103}
72104
73- impl Display for Signature {
105+ impl < D > Display for Signature < D > {
74106 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
75107 write ! ( f, "{:X}" , self )
76108 }
77109}
78110
79111#[ cfg( feature = "serde" ) ]
80- impl Serialize for Signature {
112+ impl < D > Serialize for Signature < D > {
81113 fn serialize < S > ( & self , serializer : S ) -> core:: result:: Result < S :: Ok , S :: Error >
82114 where
83115 S : serdect:: serde:: Serializer ,
@@ -87,7 +119,7 @@ impl Serialize for Signature {
87119}
88120
89121#[ cfg( feature = "serde" ) ]
90- impl < ' de > Deserialize < ' de > for Signature {
122+ impl < ' de , Di > Deserialize < ' de > for Signature < Di > {
91123 fn deserialize < D > ( deserializer : D ) -> core:: result:: Result < Self , D :: Error >
92124 where
93125 D : serdect:: serde:: Deserializer < ' de > ,
@@ -99,6 +131,13 @@ impl<'de> Deserialize<'de> for Signature {
99131 }
100132}
101133
134+ impl < D > PrehashSignature for Signature < D >
135+ where
136+ D : Digest ,
137+ {
138+ type Digest = D ;
139+ }
140+
102141#[ cfg( test) ]
103142mod tests {
104143 #[ test]
@@ -108,6 +147,7 @@ mod tests {
108147 use serde_test:: { assert_tokens, Configure , Token } ;
109148 let signature = Signature {
110149 inner : BoxedUint :: from ( 42u32 ) ,
150+ _digest : PhantomData :: < ( ) > ,
111151 } ;
112152
113153 let tokens = [ Token :: Str ( "000000000000002a" ) ] ;
0 commit comments