Update deps.

This commit is contained in:
Thomas Gideon 2018-03-12 09:38:01 -04:00
parent 5cb408e16a
commit ccc6657083
3 changed files with 22 additions and 11 deletions

View file

@ -13,8 +13,8 @@ categories = ["cryptography", "authentication", "web-programming",
license = "MIT"
[dependencies]
base64 = "0.4"
openssl = "0.9.11"
base64 = "0.9"
openssl = "0.10"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"

View file

@ -35,7 +35,7 @@ fn sign_hmac(data: &str, key: &[u8], digest: MessageDigest) -> Result<String> {
let mut signer = Signer::new(digest, &secret_key)?;
signer.update(data.as_bytes())?;
let mac = signer.finish()?;
let mac = signer.sign_to_vec()?;
Ok(encode_config(&mac, URL_SAFE_NO_PAD))
}
@ -45,7 +45,7 @@ fn sign_rsa(data: &str, key: &[u8], digest: MessageDigest) -> Result<String> {
let mut signer = Signer::new(digest, &pkey)?;
signer.update(data.as_bytes())?;
let sig = signer.finish()?;
let sig = signer.sign_to_vec()?;
Ok(encode_config(&sig, URL_SAFE_NO_PAD))
}
@ -56,7 +56,7 @@ fn verify_hmac(target: &str, data: &str, key: &[u8], digest: MessageDigest) -> R
let mut signer = Signer::new(digest, &secret_key)?;
signer.update(data.as_bytes())?;
let mac = signer.finish()?;
let mac = signer.sign_to_vec()?;
Ok(memcmp::eq(&mac, &target_bytes))
}
@ -67,7 +67,7 @@ fn verify_rsa(signature: &str, data: &str, key: &[u8], digest: MessageDigest) ->
let pkey = PKey::from_rsa(public_key)?;
let mut verifier = Verifier::new(digest, &pkey)?;
verifier.update(data.as_bytes())?;
Ok(verifier.finish(&signature_bytes)?)
Ok(verifier.verify(&signature_bytes)?)
}
#[cfg(test)]
@ -97,9 +97,20 @@ pub mod tests {
let keypair = openssl::rsa::Rsa::generate(2048).unwrap();
let sig = sign(&*data, &keypair.private_key_to_pem().unwrap(), &Algorithm::RS256).unwrap();
let sig = sign(
&*data,
&keypair.private_key_to_pem().unwrap(),
&Algorithm::RS256,
).unwrap();
assert!(verify(&sig, &*data, &keypair.public_key_to_pem().unwrap(), &Algorithm::RS256).unwrap());
assert!(
verify(
&sig,
&*data,
&keypair.public_key_to_pem().unwrap(),
&Algorithm::RS256
).unwrap()
);
}
#[test]

View file

@ -1,4 +1,4 @@
use base64::Base64Error;
use base64::DecodeError;
use serde_json;
use std::error;
use std::fmt;
@ -12,7 +12,7 @@ pub enum Error {
/// String encoding errors.
Utf8(FromUtf8Error),
/// Base64 encoding or decoding errors.
Base64(Base64Error),
Base64(DecodeError),
/// JSON parsing or stringifying errors.
JSON(serde_json::Error),
/// Errors from RSA operations.
@ -62,6 +62,6 @@ macro_rules! error_wrap {
}
error_wrap!(FromUtf8Error, Error::Utf8);
error_wrap!(Base64Error, Error::Base64);
error_wrap!(DecodeError, Error::Base64);
error_wrap!(serde_json::Error, Error::JSON);
error_wrap!(ErrorStack, Error::Crypto);