@@ -11,140 +11,68 @@ use crate::crypto::{JwtSigner, JwtVerifier};
11
11
use crate :: errors:: Result ;
12
12
use crate :: { Algorithm , DecodingKey , EncodingKey } ;
13
13
14
- pub struct Hs256Signer ( hmac:: Key ) ;
15
-
16
- impl Hs256Signer {
17
- pub ( crate ) fn new ( encoding_key : & EncodingKey ) -> Result < Self > {
18
- Ok ( Self ( hmac:: Key :: new (
19
- hmac:: HMAC_SHA256 ,
20
- try_get_hmac_secret_from_encoding_key ( encoding_key) ?,
21
- ) ) )
22
- }
23
- }
24
-
25
- impl Signer < Vec < u8 > > for Hs256Signer {
26
- fn try_sign ( & self , msg : & [ u8 ] ) -> std:: result:: Result < Vec < u8 > , signature:: Error > {
27
- Ok ( hmac:: sign ( & self . 0 , msg) . as_ref ( ) . to_vec ( ) )
28
- }
29
- }
30
-
31
- impl JwtSigner for Hs256Signer {
32
- fn algorithm ( & self ) -> Algorithm {
33
- Algorithm :: HS256
34
- }
35
- }
36
-
37
- pub struct Hs256Verifier ( hmac:: Key ) ;
38
-
39
- impl Hs256Verifier {
40
- pub ( crate ) fn new ( decoding_key : & DecodingKey ) -> Result < Self > {
41
- Ok ( Self ( hmac:: Key :: new (
42
- hmac:: HMAC_SHA256 ,
43
- try_get_hmac_secret_from_decoding_key ( decoding_key) ?,
44
- ) ) )
45
- }
46
- }
47
-
48
- impl Verifier < Vec < u8 > > for Hs256Verifier {
49
- fn verify ( & self , msg : & [ u8 ] , signature : & Vec < u8 > ) -> std:: result:: Result < ( ) , signature:: Error > {
50
- hmac:: verify ( & self . 0 , msg, signature) . map_err ( signature:: Error :: from_source)
51
- }
52
- }
53
-
54
- impl JwtVerifier for Hs256Verifier {
55
- fn algorithm ( & self ) -> Algorithm {
56
- Algorithm :: HS256
57
- }
58
- }
59
-
60
- pub struct Hs384Signer ( hmac:: Key ) ;
61
-
62
- impl Hs384Signer {
63
- pub ( crate ) fn new ( encoding_key : & EncodingKey ) -> Result < Self > {
64
- Ok ( Self ( hmac:: Key :: new (
65
- hmac:: HMAC_SHA384 ,
66
- try_get_hmac_secret_from_encoding_key ( encoding_key) ?,
67
- ) ) )
68
- }
69
- }
70
-
71
- impl Signer < Vec < u8 > > for Hs384Signer {
72
- fn try_sign ( & self , msg : & [ u8 ] ) -> std:: result:: Result < Vec < u8 > , signature:: Error > {
73
- Ok ( hmac:: sign ( & self . 0 , msg) . as_ref ( ) . to_vec ( ) )
74
- }
75
- }
76
-
77
- impl JwtSigner for Hs384Signer {
78
- fn algorithm ( & self ) -> Algorithm {
79
- Algorithm :: HS384
80
- }
81
- }
82
-
83
- pub struct Hs384Verifier ( hmac:: Key ) ;
84
-
85
- impl Hs384Verifier {
86
- pub ( crate ) fn new ( decoding_key : & DecodingKey ) -> Result < Self > {
87
- Ok ( Self ( hmac:: Key :: new (
88
- hmac:: HMAC_SHA384 ,
89
- try_get_hmac_secret_from_decoding_key ( decoding_key) ?,
90
- ) ) )
91
- }
92
- }
93
-
94
- impl Verifier < Vec < u8 > > for Hs384Verifier {
95
- fn verify ( & self , msg : & [ u8 ] , signature : & Vec < u8 > ) -> std:: result:: Result < ( ) , signature:: Error > {
96
- hmac:: verify ( & self . 0 , msg, signature) . map_err ( signature:: Error :: from_source)
97
- }
98
- }
99
-
100
- impl JwtVerifier for Hs384Verifier {
101
- fn algorithm ( & self ) -> Algorithm {
102
- Algorithm :: HS384
103
- }
104
- }
105
-
106
- pub struct Hs512Signer ( hmac:: Key ) ;
107
-
108
- impl Hs512Signer {
109
- pub ( crate ) fn new ( encoding_key : & EncodingKey ) -> Result < Self > {
110
- Ok ( Self ( hmac:: Key :: new (
111
- hmac:: HMAC_SHA512 ,
112
- try_get_hmac_secret_from_encoding_key ( encoding_key) ?,
113
- ) ) )
114
- }
115
- }
116
-
117
- impl Signer < Vec < u8 > > for Hs512Signer {
118
- fn try_sign ( & self , msg : & [ u8 ] ) -> std:: result:: Result < Vec < u8 > , signature:: Error > {
119
- Ok ( hmac:: sign ( & self . 0 , msg) . as_ref ( ) . to_vec ( ) )
120
- }
121
- }
122
-
123
- impl JwtSigner for Hs512Signer {
124
- fn algorithm ( & self ) -> Algorithm {
125
- Algorithm :: HS512
126
- }
127
- }
128
-
129
- pub struct Hs512Verifier ( hmac:: Key ) ;
130
-
131
- impl Hs512Verifier {
132
- pub ( crate ) fn new ( decoding_key : & DecodingKey ) -> Result < Self > {
133
- Ok ( Self ( hmac:: Key :: new (
134
- hmac:: HMAC_SHA512 ,
135
- try_get_hmac_secret_from_decoding_key ( decoding_key) ?,
136
- ) ) )
137
- }
138
- }
139
-
140
- impl Verifier < Vec < u8 > > for Hs512Verifier {
141
- fn verify ( & self , msg : & [ u8 ] , signature : & Vec < u8 > ) -> std:: result:: Result < ( ) , signature:: Error > {
142
- hmac:: verify ( & self . 0 , msg, signature) . map_err ( signature:: Error :: from_source)
143
- }
144
- }
145
-
146
- impl JwtVerifier for Hs512Verifier {
147
- fn algorithm ( & self ) -> Algorithm {
148
- Algorithm :: HS512
149
- }
150
- }
14
+ macro_rules! define_hmac_signer {
15
+ ( $name: ident, $alg: expr, $hmac_alg: expr) => {
16
+ pub struct $name( hmac:: Key ) ;
17
+
18
+ impl $name {
19
+ pub ( crate ) fn new( encoding_key: & EncodingKey ) -> Result <Self > {
20
+ Ok ( Self ( hmac:: Key :: new(
21
+ $hmac_alg,
22
+ try_get_hmac_secret_from_encoding_key( encoding_key) ?,
23
+ ) ) )
24
+ }
25
+ }
26
+
27
+ impl Signer <Vec <u8 >> for $name {
28
+ fn try_sign( & self , msg: & [ u8 ] ) -> std:: result:: Result <Vec <u8 >, signature:: Error > {
29
+ Ok ( hmac:: sign( & self . 0 , msg) . as_ref( ) . to_vec( ) )
30
+ }
31
+ }
32
+
33
+ impl JwtSigner for $name {
34
+ fn algorithm( & self ) -> Algorithm {
35
+ $alg
36
+ }
37
+ }
38
+ } ;
39
+ }
40
+
41
+ macro_rules! define_hmac_verifier {
42
+ ( $name: ident, $alg: expr, $hmac_alg: expr) => {
43
+ pub struct $name( hmac:: Key ) ;
44
+
45
+ impl $name {
46
+ pub ( crate ) fn new( decoding_key: & DecodingKey ) -> Result <Self > {
47
+ Ok ( Self ( hmac:: Key :: new(
48
+ $hmac_alg,
49
+ try_get_hmac_secret_from_decoding_key( decoding_key) ?,
50
+ ) ) )
51
+ }
52
+ }
53
+
54
+ impl Verifier <Vec <u8 >> for $name {
55
+ fn verify(
56
+ & self ,
57
+ msg: & [ u8 ] ,
58
+ signature: & Vec <u8 >,
59
+ ) -> std:: result:: Result <( ) , signature:: Error > {
60
+ hmac:: verify( & self . 0 , msg, signature) . map_err( signature:: Error :: from_source)
61
+ }
62
+ }
63
+
64
+ impl JwtVerifier for $name {
65
+ fn algorithm( & self ) -> Algorithm {
66
+ $alg
67
+ }
68
+ }
69
+ } ;
70
+ }
71
+
72
+ define_hmac_signer ! ( Hs256Signer , Algorithm :: HS256 , hmac:: HMAC_SHA256 ) ;
73
+ define_hmac_signer ! ( Hs384Signer , Algorithm :: HS384 , hmac:: HMAC_SHA384 ) ;
74
+ define_hmac_signer ! ( Hs512Signer , Algorithm :: HS512 , hmac:: HMAC_SHA512 ) ;
75
+
76
+ define_hmac_verifier ! ( Hs256Verifier , Algorithm :: HS256 , hmac:: HMAC_SHA256 ) ;
77
+ define_hmac_verifier ! ( Hs384Verifier , Algorithm :: HS384 , hmac:: HMAC_SHA384 ) ;
78
+ define_hmac_verifier ! ( Hs512Verifier , Algorithm :: HS512 , hmac:: HMAC_SHA512 ) ;
0 commit comments