@@ -6,25 +6,28 @@ use sha2::{Sha256, Sha384, Sha512};
6
6
use signature:: { Signer , Verifier } ;
7
7
8
8
use crate :: crypto:: { JwtSigner , JwtVerifier } ;
9
+ use crate :: decoding:: try_get_hmac_secret_from_decoding_key;
10
+ use crate :: encoding:: try_get_hmac_secret_from_encoding_key;
9
11
use crate :: errors:: Result ;
10
- use crate :: { Algorithm , HmacSecret } ;
12
+ use crate :: { Algorithm , DecodingKey , EncodingKey } ;
11
13
12
14
type HmacSha256 = Hmac < Sha256 > ;
13
15
type HmacSha384 = Hmac < Sha384 > ;
14
16
type HmacSha512 = Hmac < Sha512 > ;
15
17
16
- pub struct Hs256 ( HmacSha256 ) ;
18
+ pub struct Hs256Signer ( HmacSha256 ) ;
17
19
18
- impl Hs256 {
19
- pub ( crate ) fn new ( secret : HmacSecret ) -> Result < Self > {
20
- let inner = HmacSha256 :: new_from_slice ( & secret)
21
- . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
20
+ impl Hs256Signer {
21
+ pub ( crate ) fn new ( encoding_key : & EncodingKey ) -> Result < Self > {
22
+ let inner =
23
+ HmacSha256 :: new_from_slice ( try_get_hmac_secret_from_encoding_key ( encoding_key) ?)
24
+ . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
22
25
23
26
Ok ( Self ( inner) )
24
27
}
25
28
}
26
29
27
- impl Signer < Vec < u8 > > for Hs256 {
30
+ impl Signer < Vec < u8 > > for Hs256Signer {
28
31
fn try_sign ( & self , msg : & [ u8 ] ) -> std:: result:: Result < Vec < u8 > , signature:: Error > {
29
32
let mut signer = self . 0 . clone ( ) ;
30
33
signer. reset ( ) ;
@@ -34,13 +37,25 @@ impl Signer<Vec<u8>> for Hs256 {
34
37
}
35
38
}
36
39
37
- impl JwtSigner for Hs256 {
40
+ impl JwtSigner for Hs256Signer {
38
41
fn algorithm ( & self ) -> Algorithm {
39
42
Algorithm :: HS256
40
43
}
41
44
}
42
45
43
- impl Verifier < Vec < u8 > > for Hs256 {
46
+ pub struct Hs256Verifier ( HmacSha256 ) ;
47
+
48
+ impl Hs256Verifier {
49
+ pub ( crate ) fn new ( decoding_key : & DecodingKey ) -> Result < Self > {
50
+ let inner =
51
+ HmacSha256 :: new_from_slice ( & try_get_hmac_secret_from_decoding_key ( decoding_key) ?)
52
+ . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
53
+
54
+ Ok ( Self ( inner) )
55
+ }
56
+ }
57
+
58
+ impl Verifier < Vec < u8 > > for Hs256Verifier {
44
59
fn verify ( & self , msg : & [ u8 ] , signature : & Vec < u8 > ) -> std:: result:: Result < ( ) , signature:: Error > {
45
60
let mut verifier = self . 0 . clone ( ) ;
46
61
verifier. reset ( ) ;
@@ -50,24 +65,25 @@ impl Verifier<Vec<u8>> for Hs256 {
50
65
}
51
66
}
52
67
53
- impl JwtVerifier for Hs256 {
68
+ impl JwtVerifier for Hs256Verifier {
54
69
fn algorithm ( & self ) -> Algorithm {
55
70
Algorithm :: HS256
56
71
}
57
72
}
58
73
59
- pub ( crate ) struct Hs384 ( HmacSha384 ) ;
74
+ pub struct Hs384Signer ( HmacSha384 ) ;
60
75
61
- impl Hs384 {
62
- pub ( crate ) fn new ( secret : HmacSecret ) -> Result < Self > {
63
- let inner = HmacSha384 :: new_from_slice ( & secret)
64
- . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
76
+ impl Hs384Signer {
77
+ pub ( crate ) fn new ( encoding_key : & EncodingKey ) -> Result < Self > {
78
+ let inner =
79
+ HmacSha384 :: new_from_slice ( try_get_hmac_secret_from_encoding_key ( encoding_key) ?)
80
+ . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
65
81
66
82
Ok ( Self ( inner) )
67
83
}
68
84
}
69
85
70
- impl Signer < Vec < u8 > > for Hs384 {
86
+ impl Signer < Vec < u8 > > for Hs384Signer {
71
87
fn try_sign ( & self , msg : & [ u8 ] ) -> std:: result:: Result < Vec < u8 > , signature:: Error > {
72
88
let mut signer = self . 0 . clone ( ) ;
73
89
signer. reset ( ) ;
@@ -77,13 +93,25 @@ impl Signer<Vec<u8>> for Hs384 {
77
93
}
78
94
}
79
95
80
- impl JwtSigner for Hs384 {
96
+ impl JwtSigner for Hs384Signer {
81
97
fn algorithm ( & self ) -> Algorithm {
82
98
Algorithm :: HS384
83
99
}
84
100
}
85
101
86
- impl Verifier < Vec < u8 > > for Hs384 {
102
+ pub struct Hs384Verifier ( HmacSha384 ) ;
103
+
104
+ impl Hs384Verifier {
105
+ pub ( crate ) fn new ( decoding_key : & DecodingKey ) -> Result < Self > {
106
+ let inner =
107
+ HmacSha384 :: new_from_slice ( & try_get_hmac_secret_from_decoding_key ( decoding_key) ?)
108
+ . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
109
+
110
+ Ok ( Self ( inner) )
111
+ }
112
+ }
113
+
114
+ impl Verifier < Vec < u8 > > for Hs384Verifier {
87
115
fn verify ( & self , msg : & [ u8 ] , signature : & Vec < u8 > ) -> std:: result:: Result < ( ) , signature:: Error > {
88
116
let mut verifier = self . 0 . clone ( ) ;
89
117
verifier. reset ( ) ;
@@ -93,24 +121,25 @@ impl Verifier<Vec<u8>> for Hs384 {
93
121
}
94
122
}
95
123
96
- impl JwtVerifier for Hs384 {
124
+ impl JwtVerifier for Hs384Verifier {
97
125
fn algorithm ( & self ) -> Algorithm {
98
126
Algorithm :: HS384
99
127
}
100
128
}
101
129
102
- pub struct Hs512 ( HmacSha512 ) ;
130
+ pub struct Hs512Signer ( HmacSha512 ) ;
103
131
104
- impl Hs512 {
105
- pub ( crate ) fn new ( secret : HmacSecret ) -> Result < Self > {
106
- let inner = HmacSha512 :: new_from_slice ( & secret)
107
- . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
132
+ impl Hs512Signer {
133
+ pub ( crate ) fn new ( encoding_key : & EncodingKey ) -> Result < Self > {
134
+ let inner =
135
+ HmacSha512 :: new_from_slice ( try_get_hmac_secret_from_encoding_key ( encoding_key) ?)
136
+ . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
108
137
109
138
Ok ( Self ( inner) )
110
139
}
111
140
}
112
141
113
- impl Signer < Vec < u8 > > for Hs512 {
142
+ impl Signer < Vec < u8 > > for Hs512Signer {
114
143
fn try_sign ( & self , msg : & [ u8 ] ) -> std:: result:: Result < Vec < u8 > , signature:: Error > {
115
144
let mut signer = self . 0 . clone ( ) ;
116
145
signer. reset ( ) ;
@@ -120,13 +149,25 @@ impl Signer<Vec<u8>> for Hs512 {
120
149
}
121
150
}
122
151
123
- impl JwtSigner for Hs512 {
152
+ impl JwtSigner for Hs512Signer {
124
153
fn algorithm ( & self ) -> Algorithm {
125
154
Algorithm :: HS512
126
155
}
127
156
}
128
157
129
- impl Verifier < Vec < u8 > > for Hs512 {
158
+ pub struct Hs512Verifier ( HmacSha512 ) ;
159
+
160
+ impl Hs512Verifier {
161
+ pub ( crate ) fn new ( decoding_key : & DecodingKey ) -> Result < Self > {
162
+ let inner =
163
+ HmacSha512 :: new_from_slice ( & try_get_hmac_secret_from_decoding_key ( decoding_key) ?)
164
+ . map_err ( |_e| crate :: errors:: ErrorKind :: InvalidKeyFormat ) ?;
165
+
166
+ Ok ( Self ( inner) )
167
+ }
168
+ }
169
+
170
+ impl Verifier < Vec < u8 > > for Hs512Verifier {
130
171
fn verify ( & self , msg : & [ u8 ] , signature : & Vec < u8 > ) -> std:: result:: Result < ( ) , signature:: Error > {
131
172
let mut verifier = self . 0 . clone ( ) ;
132
173
verifier. reset ( ) ;
@@ -136,7 +177,7 @@ impl Verifier<Vec<u8>> for Hs512 {
136
177
}
137
178
}
138
179
139
- impl JwtVerifier for Hs512 {
180
+ impl JwtVerifier for Hs512Verifier {
140
181
fn algorithm ( & self ) -> Algorithm {
141
182
Algorithm :: HS512
142
183
}
0 commit comments