diff --git a/src/encoder.rs b/src/encoder.rs index 97abb44..ad391d5 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -292,9 +292,11 @@ impl SdObjectEncoder { } fn gen_rand(len: usize) -> String { + let mut bytes = vec![0; len]; let mut rng = rand::thread_rng(); - let random_bytes: Vec = (0..len).map(|_| rng.gen()).collect(); - multibase::Base::Base64Url.encode(random_bytes) + rng.fill(&mut bytes[..]); + + multibase::Base::Base64Url.encode(bytes) } /// Returns a reference to the internal object. diff --git a/src/error.rs b/src/error.rs index 957418d..f67f942 100644 --- a/src/error.rs +++ b/src/error.rs @@ -37,6 +37,6 @@ pub enum Error { #[error("{0}")] Unspecified(String), - #[error("salt size must be greater or equal 16")] + #[error("salt size must be greater than or equal to 16")] InvalidSaltSize, } diff --git a/src/lib.rs b/src/lib.rs index 4d0cb27..f7686ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,6 @@ // Copyright 2020-2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -#[cfg(test)] -mod api_test; mod decoder; mod disclosure; mod encoder; diff --git a/src/api_test.rs b/tests/api_test.rs similarity index 97% rename from src/api_test.rs rename to tests/api_test.rs index fb89046..9f02987 100644 --- a/src/api_test.rs +++ b/tests/api_test.rs @@ -11,10 +11,10 @@ use serde_json::json; use serde_json::Map; use serde_json::Value; -use crate::Disclosure; -use crate::SdJwt; -use crate::SdObjectDecoder; -use crate::SdObjectEncoder; +use sd_jwt::Disclosure; +use sd_jwt::SdJwt; +use sd_jwt::SdObjectDecoder; +use sd_jwt::SdObjectEncoder; #[test] fn test_complex_structure() { @@ -169,7 +169,7 @@ fn decode() { // Values taken from https://www.ietf.org/archive/id/draft-ietf-oauth-selective-disclosure-jwt-06.html#name-example-2-handling-structur let sd_jwt = "eyJhbGciOiAiRVMyNTYifQ.eyJfc2QiOiBbIkM5aW5wNllvUmFFWFI0Mjd6WUpQN1FyazFXSF84YmR3T0FfWVVyVW5HUVUiLCAiS3VldDF5QWE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FhRnY3R3JNWDQiXX0sICJfc2RfYWxnIjogInNoYS0yNTYifQ.IjE4EfnYu1RZ1uz6yqtFh5Lppq36VC4VeSr-hLDFpZ9zqBNmMrT5JHLLXTuMJqKQp3NIzDsLaft4GK5bYyfqhg~WyJHMDJOU3JRZmpGWFE3SW8wOXN5YWpBIiwgInJlZ2lvbiIsICJcdTZlMmZcdTUzM2EiXQ~WyJsa2x4RjVqTVlsR1RQVW92TU5JdkNBIiwgImNvdW50cnkiLCAiSlAiXQ~"; let sd_jwt: SdJwt = SdJwt::parse(sd_jwt).unwrap(); - let (payload, _header) = jwt::decode_with_verifier(&sd_jwt.jwt, &DeocyJwsVerifier {}).unwrap(); + let (payload, _header) = jwt::decode_with_verifier(&sd_jwt.jwt, &DecoyJwsVerifier {}).unwrap(); let decoder = SdObjectDecoder::new_with_sha256(); let decoded: Map = decoder.decode(payload.claims_set(), &sd_jwt.disclosures).unwrap(); let expected_object = json!({ @@ -202,8 +202,8 @@ impl JwsAlgorithm for DecoyJwsAlgorithm { } #[derive(Debug, Clone)] -struct DeocyJwsVerifier; -impl JwsVerifier for DeocyJwsVerifier { +struct DecoyJwsVerifier; +impl JwsVerifier for DecoyJwsVerifier { fn algorithm(&self) -> &dyn josekit::jws::JwsAlgorithm { &DecoyJwsAlgorithm {} }