fmt + clippy

This commit is contained in:
Vincent Prouillet 2020-11-17 14:45:07 +01:00
parent ea4ed55551
commit 2662f6ad1f
4 changed files with 64 additions and 26 deletions

View File

@ -18,10 +18,15 @@ fn round_trip_sign_verification_pk8() {
let pubkey = include_bytes!("public_ecdsa_key.pk8"); let pubkey = include_bytes!("public_ecdsa_key.pk8");
let encrypted = let encrypted =
sign("hello world".as_bytes(), &EncodingKey::from_ec_der(privkey), Algorithm::ES256).unwrap(); sign(b"hello world", &EncodingKey::from_ec_der(privkey), Algorithm::ES256)
let is_valid =
verify(&encrypted, "hello world".as_bytes(), &DecodingKey::from_ec_der(pubkey), Algorithm::ES256)
.unwrap(); .unwrap();
let is_valid = verify(
&encrypted,
b"hello world",
&DecodingKey::from_ec_der(pubkey),
Algorithm::ES256,
)
.unwrap();
assert!(is_valid); assert!(is_valid);
} }
@ -29,12 +34,15 @@ fn round_trip_sign_verification_pk8() {
fn round_trip_sign_verification_pem() { fn round_trip_sign_verification_pem() {
let privkey_pem = include_bytes!("private_ecdsa_key.pem"); let privkey_pem = include_bytes!("private_ecdsa_key.pem");
let pubkey_pem = include_bytes!("public_ecdsa_key.pem"); let pubkey_pem = include_bytes!("public_ecdsa_key.pem");
let encrypted = let encrypted = sign(
sign("hello world".as_bytes(), &EncodingKey::from_ec_pem(privkey_pem).unwrap(), Algorithm::ES256) b"hello world",
.unwrap(); &EncodingKey::from_ec_pem(privkey_pem).unwrap(),
Algorithm::ES256,
)
.unwrap();
let is_valid = verify( let is_valid = verify(
&encrypted, &encrypted,
"hello world".as_bytes(), b"hello world",
&DecodingKey::from_ec_pem(pubkey_pem).unwrap(), &DecodingKey::from_ec_pem(pubkey_pem).unwrap(),
Algorithm::ES256, Algorithm::ES256,
) )

View File

@ -18,10 +18,15 @@ fn round_trip_sign_verification_pk8() {
let pubkey = include_bytes!("public_ed25519_key.pk8"); let pubkey = include_bytes!("public_ed25519_key.pk8");
let encrypted = let encrypted =
sign("hello world".as_bytes(), &EncodingKey::from_ed_der(privkey), Algorithm::EdDSA).unwrap(); sign(b"hello world", &EncodingKey::from_ed_der(privkey), Algorithm::EdDSA)
let is_valid =
verify(&encrypted, "hello world".as_bytes(), &DecodingKey::from_ed_der(pubkey), Algorithm::EdDSA)
.unwrap(); .unwrap();
let is_valid = verify(
&encrypted,
b"hello world",
&DecodingKey::from_ed_der(pubkey),
Algorithm::EdDSA,
)
.unwrap();
assert!(is_valid); assert!(is_valid);
} }
@ -29,12 +34,15 @@ fn round_trip_sign_verification_pk8() {
fn round_trip_sign_verification_pem() { fn round_trip_sign_verification_pem() {
let privkey_pem = include_bytes!("private_ed25519_key.pem"); let privkey_pem = include_bytes!("private_ed25519_key.pem");
let pubkey_pem = include_bytes!("public_ed25519_key.pem"); let pubkey_pem = include_bytes!("public_ed25519_key.pem");
let encrypted = let encrypted = sign(
sign("hello world".as_bytes(), &EncodingKey::from_ed_pem(privkey_pem).unwrap(), Algorithm::EdDSA) b"hello world",
.unwrap(); &EncodingKey::from_ed_pem(privkey_pem).unwrap(),
Algorithm::EdDSA,
)
.unwrap();
let is_valid = verify( let is_valid = verify(
&encrypted, &encrypted,
"hello world".as_bytes(), b"hello world",
&DecodingKey::from_ed_pem(pubkey_pem).unwrap(), &DecodingKey::from_ed_pem(pubkey_pem).unwrap(),
Algorithm::EdDSA, Algorithm::EdDSA,
) )

View File

@ -17,7 +17,8 @@ pub struct Claims {
#[test] #[test]
fn sign_hs256() { fn sign_hs256() {
let result = let result =
sign("hello world".as_bytes(), &EncodingKey::from_secret(b"secret"), Algorithm::HS256).unwrap(); sign(b"hello world", &EncodingKey::from_secret(b"secret"), Algorithm::HS256)
.unwrap();
let expected = "c0zGLzKEFWj0VxWuufTXiRMk5tlI5MbGDAYhzaxIYjo"; let expected = "c0zGLzKEFWj0VxWuufTXiRMk5tlI5MbGDAYhzaxIYjo";
assert_eq!(result, expected); assert_eq!(result, expected);
} }
@ -25,8 +26,13 @@ fn sign_hs256() {
#[test] #[test]
fn verify_hs256() { fn verify_hs256() {
let sig = "c0zGLzKEFWj0VxWuufTXiRMk5tlI5MbGDAYhzaxIYjo"; let sig = "c0zGLzKEFWj0VxWuufTXiRMk5tlI5MbGDAYhzaxIYjo";
let valid = let valid = verify(
verify(sig, "hello world".as_bytes(), &DecodingKey::from_secret(b"secret"), Algorithm::HS256).unwrap(); sig,
b"hello world",
&DecodingKey::from_secret(b"secret"),
Algorithm::HS256,
)
.unwrap();
assert!(valid); assert!(valid);
} }

View File

@ -28,10 +28,15 @@ fn round_trip_sign_verification_pem_pkcs1() {
for &alg in RSA_ALGORITHMS { for &alg in RSA_ALGORITHMS {
let encrypted = let encrypted =
sign("hello world".as_bytes(), &EncodingKey::from_rsa_pem(privkey_pem).unwrap(), alg).unwrap(); sign(b"hello world", &EncodingKey::from_rsa_pem(privkey_pem).unwrap(), alg)
let is_valid =
verify(&encrypted, "hello world".as_bytes(), &DecodingKey::from_rsa_pem(pubkey_pem).unwrap(), alg)
.unwrap(); .unwrap();
let is_valid = verify(
&encrypted,
b"hello world",
&DecodingKey::from_rsa_pem(pubkey_pem).unwrap(),
alg,
)
.unwrap();
assert!(is_valid); assert!(is_valid);
} }
} }
@ -43,10 +48,15 @@ fn round_trip_sign_verification_pem_pkcs8() {
for &alg in RSA_ALGORITHMS { for &alg in RSA_ALGORITHMS {
let encrypted = let encrypted =
sign("hello world".as_bytes(), &EncodingKey::from_rsa_pem(privkey_pem).unwrap(), alg).unwrap(); sign(b"hello world", &EncodingKey::from_rsa_pem(privkey_pem).unwrap(), alg)
let is_valid =
verify(&encrypted, "hello world".as_bytes(), &DecodingKey::from_rsa_pem(pubkey_pem).unwrap(), alg)
.unwrap(); .unwrap();
let is_valid = verify(
&encrypted,
b"hello world",
&DecodingKey::from_rsa_pem(pubkey_pem).unwrap(),
alg,
)
.unwrap();
assert!(is_valid); assert!(is_valid);
} }
} }
@ -57,9 +67,15 @@ fn round_trip_sign_verification_der() {
let pubkey_der = include_bytes!("public_rsa_key.der"); let pubkey_der = include_bytes!("public_rsa_key.der");
for &alg in RSA_ALGORITHMS { for &alg in RSA_ALGORITHMS {
let encrypted = sign("hello world".as_bytes(), &EncodingKey::from_rsa_der(privkey_der), alg).unwrap(); let encrypted =
let is_valid = sign(b"hello world", &EncodingKey::from_rsa_der(privkey_der), alg).unwrap();
verify(&encrypted, "hello world".as_bytes(), &DecodingKey::from_rsa_der(pubkey_der), alg).unwrap(); let is_valid = verify(
&encrypted,
b"hello world",
&DecodingKey::from_rsa_der(pubkey_der),
alg,
)
.unwrap();
assert!(is_valid); assert!(is_valid);
} }
} }