Skip to content

Commit 42c6baa

Browse files
committed
remove deprecated verify_slices_are_equal
1 parent 67a5453 commit 42c6baa

File tree

3 files changed

+6
-16
lines changed

3 files changed

+6
-16
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ base64 = "0.22"
2626
# For PEM decoding
2727
pem = { version = "3", optional = true }
2828
simple_asn1 = { version = "0.6", optional = true }
29-
openssl = "0.10.71"
3029

3130
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
3231
ring = { version = "0.17.4", features = ["std"] }

src/crypto/mod.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ring::{hmac, signature};
33
use crate::algorithms::Algorithm;
44
use crate::decoding::{DecodingKey, DecodingKeyKind};
55
use crate::encoding::EncodingKey;
6-
use crate::errors::{Error, Result};
6+
use crate::errors::Result;
77
use crate::serialization::{b64_decode, b64_encode};
88

99
pub(crate) mod ecdsa;
@@ -20,17 +20,6 @@ fn alg_to_hmac(alg: Algorithm) -> hmac::Algorithm {
2020
}
2121
}
2222

23-
/// Returns `Ok(())` if `a == b` and `Err(error::Unspecified)` otherwise.
24-
pub fn verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<()> {
25-
if b.len() != a.len() {
26-
return Err(Error::from(ring::error::Unspecified));
27-
}
28-
match openssl::memcmp::eq(a, b) {
29-
true => Ok(()),
30-
_ => Err(Error::from(ring::error::Unspecified)),
31-
}
32-
}
33-
3423
/// The actual HS signing + encoding
3524
/// Could be in its own file to match RSA/EC but it's 2 lines...
3625
pub(crate) fn sign_hmac(alg: hmac::Algorithm, key: &[u8], message: &[u8]) -> String {
@@ -94,8 +83,10 @@ pub fn verify(
9483
match algorithm {
9584
Algorithm::HS256 | Algorithm::HS384 | Algorithm::HS512 => {
9685
// we just re-sign the message with the key and compare if they are equal
97-
let signed = sign(message, &EncodingKey::from_secret(key.as_bytes()), algorithm)?;
98-
Ok(verify_slices_are_equal(signature.as_ref(), signed.as_ref()).is_ok())
86+
let encoding_key = &EncodingKey::from_secret(key.as_bytes());
87+
let key = &hmac::Key::new(alg_to_hmac(algorithm), encoding_key.inner());
88+
let digest = hmac::sign(key, message);
89+
Ok(hmac::verify(key, message, digest.as_ref()).is_ok())
9990
}
10091
Algorithm::ES256 | Algorithm::ES384 => verify_ring(
10192
ecdsa::alg_to_ec_verification(algorithm),

tests/hmac.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn decode_token_missing_parts() {
151151

152152
#[test]
153153
#[wasm_bindgen_test]
154-
#[should_panic(expected = "InvalidSignature")]
154+
#[should_panic(expected = "missing field `exp`")]
155155
fn decode_token_invalid_signature() {
156156
let token =
157157
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.wrong";

0 commit comments

Comments
 (0)