Skip to content

Commit 66d54be

Browse files
committed
test: Get HMAC tests passing
1 parent 6a41ba7 commit 66d54be

File tree

7 files changed

+276
-293
lines changed

7 files changed

+276
-293
lines changed

src/builder.rs

Lines changed: 0 additions & 217 deletions
This file was deleted.

src/crypto/hmac.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type HmacSha256 = Hmac<Sha256>;
1212
type HmacSha384 = Hmac<Sha384>;
1313
type HmacSha512 = Hmac<Sha512>;
1414

15+
#[derive(Debug)]
1516
pub(crate) struct HmacSecret(Vec<u8>);
1617

1718
impl HmacSecret {
@@ -111,3 +112,46 @@ impl JwtVerifier for Hs384 {
111112
Algorithm::HS384
112113
}
113114
}
115+
116+
pub struct Hs512(HmacSha512);
117+
118+
impl Hs512 {
119+
pub(crate) fn new(secret: HmacSecret) -> Result<Self> {
120+
let inner = HmacSha512::new_from_slice(&secret.0)
121+
.map_err(|_e| crate::errors::ErrorKind::InvalidKeyFormat)?;
122+
123+
Ok(Self(inner))
124+
}
125+
}
126+
127+
impl Signer<Vec<u8>> for Hs512 {
128+
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
129+
let mut signer = self.0.clone();
130+
signer.reset();
131+
signer.update(msg);
132+
133+
Ok(signer.finalize().into_bytes().to_vec())
134+
}
135+
}
136+
137+
impl JwtSigner for Hs512 {
138+
fn algorithm(&self) -> Algorithm {
139+
Algorithm::HS512
140+
}
141+
}
142+
143+
impl Verifier<Vec<u8>> for Hs512 {
144+
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> {
145+
let mut verifier = self.0.clone();
146+
verifier.reset();
147+
verifier.update(msg);
148+
149+
verifier.verify_slice(signature).map_err(|e| signature::Error::from_source(e))
150+
}
151+
}
152+
153+
impl JwtVerifier for Hs512 {
154+
fn algorithm(&self) -> Algorithm {
155+
Algorithm::HS512
156+
}
157+
}

0 commit comments

Comments
 (0)