@@ -82,6 +82,34 @@ pub trait MACLike {
82
82
fn attach_metadata ( & mut self , metadata : KeyMetadata ) -> Result < ( ) , Error > ;
83
83
fn authentication_tag ( & self , authenticated : & [ u8 ] ) -> Vec < u8 > ;
84
84
85
+ fn salt ( & self ) -> Salt {
86
+ self . metadata ( )
87
+ . as_ref ( )
88
+ . map ( |metadata| metadata. salt . clone ( ) )
89
+ . unwrap_or ( Salt :: None )
90
+ }
91
+
92
+ fn verifier_salt ( & self ) -> Salt {
93
+ match self . metadata ( ) . as_ref ( ) . map ( |metadata| & metadata. salt ) {
94
+ None => Salt :: None ,
95
+ Some ( Salt :: Signer ( salt) ) => {
96
+ let authenticated_salt = self . authentication_tag ( salt) ;
97
+ Salt :: Verifier ( authenticated_salt)
98
+ }
99
+ Some ( x @ Salt :: Verifier ( _) ) => x. clone ( ) ,
100
+ Some ( Salt :: None ) => Salt :: None ,
101
+ }
102
+ }
103
+
104
+ fn attach_salt ( & mut self , salt : Salt ) -> Result < ( ) , Error > {
105
+ let metadata = KeyMetadata {
106
+ salt,
107
+ ..Default :: default ( )
108
+ } ;
109
+ self . attach_metadata ( metadata) . unwrap ( ) ;
110
+ Ok ( ( ) )
111
+ }
112
+
85
113
fn authenticate < CustomClaims : Serialize + DeserializeOwned > (
86
114
& self ,
87
115
claims : JWTClaims < CustomClaims > ,
@@ -221,6 +249,13 @@ impl HS256Key {
221
249
}
222
250
}
223
251
252
+ pub fn generate_with_salt ( ) -> Self {
253
+ HS256Key {
254
+ key : HMACKey :: generate_with_salt ( ) ,
255
+ key_id : None ,
256
+ }
257
+ }
258
+
224
259
pub fn with_key_id ( mut self , key_id : & str ) -> Self {
225
260
self . key_id = Some ( key_id. to_string ( ) ) ;
226
261
self
@@ -283,6 +318,13 @@ impl HS512Key {
283
318
}
284
319
}
285
320
321
+ pub fn generate_with_salt ( ) -> Self {
322
+ HS512Key {
323
+ key : HMACKey :: generate_with_salt ( ) ,
324
+ key_id : None ,
325
+ }
326
+ }
327
+
286
328
pub fn with_key_id ( mut self , key_id : & str ) -> Self {
287
329
self . key_id = Some ( key_id. to_string ( ) ) ;
288
330
self
@@ -345,6 +387,13 @@ impl HS384Key {
345
387
}
346
388
}
347
389
390
+ pub fn generate_with_salt ( ) -> Self {
391
+ HS384Key {
392
+ key : HMACKey :: generate_with_salt ( ) ,
393
+ key_id : None ,
394
+ }
395
+ }
396
+
348
397
pub fn with_key_id ( mut self , key_id : & str ) -> Self {
349
398
self . key_id = Some ( key_id. to_string ( ) ) ;
350
399
self
@@ -416,6 +465,13 @@ impl Blake2bKey {
416
465
}
417
466
}
418
467
468
+ pub fn generate_with_salt ( ) -> Self {
469
+ Blake2bKey {
470
+ key : HMACKey :: generate_with_salt ( ) ,
471
+ key_id : None ,
472
+ }
473
+ }
474
+
419
475
pub fn with_key_id ( mut self , key_id : & str ) -> Self {
420
476
self . key_id = Some ( key_id. to_string ( ) ) ;
421
477
self
0 commit comments