Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nabetti1720 committed Nov 30, 2024
1 parent 0208aea commit 7b736a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions modules/llrt_crypto/src/subtle/derive_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ fn extract_derive_algorithm(ctx: &Ctx<'_>, algorithm: &Value) -> Result<DeriveAl

let salt = algorithm
.get_optional::<_, ObjectBytes>("salt")?
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'salt' property required"))?;
let salt = salt.as_bytes().to_vec();
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'salt' property required"))?
.into_bytes();

let info = algorithm
.get_optional::<_, ObjectBytes>("info")?
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'info' property required"))?;
let info = info.as_bytes().to_vec();
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'info' property required"))?
.into_bytes();

Ok(DeriveAlgorithm::Hkdf { hash, salt, info })
},
Expand All @@ -85,8 +85,8 @@ fn extract_derive_algorithm(ctx: &Ctx<'_>, algorithm: &Value) -> Result<DeriveAl

let salt = algorithm
.get_optional::<_, ObjectBytes>("salt")?
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'salt' property required"))?;
let salt = salt.as_bytes().to_vec();
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'salt' property required"))?
.into_bytes();

let iterations = algorithm.get_optional("iterations")?.ok_or_else(|| {
Exception::throw_type(ctx, "algorithm 'iterations' property required")
Expand Down
3 changes: 1 addition & 2 deletions modules/llrt_crypto/src/subtle/generate_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ fn extract_generate_key_algorithm(

let public_exponent = algorithm.get_optional::<_, ObjectBytes>("publicExponent")?.ok_or_else(|| {
Exception::throw_type(ctx, "algorithm 'publicExponent' property required")
})?;
let public_exponent = public_exponent.as_bytes().to_vec();
})?.into_bytes();

Ok((name, KeyGenAlgorithm::Rsa { modulus_length, public_exponent }))
},
Expand Down
19 changes: 10 additions & 9 deletions modules/llrt_crypto/src/subtle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,34 @@ fn extract_algorithm_object(ctx: &Ctx<'_>, algorithm: &Value) -> Result<Algorith
"AES-GCM" => {
let iv = algorithm
.get_optional::<_, ObjectBytes>("iv")?
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'iv' property required"))?;
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'iv' property required"))?
.into_bytes();

Ok(Algorithm::AesGcm(iv.as_bytes().to_vec()))
Ok(Algorithm::AesGcm(iv))
},
"AES-CBC" => {
let iv = algorithm
.get_optional::<_, ObjectBytes>("iv")?
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'iv' property required"))?;
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'iv' property required"))?
.into_bytes();

Ok(Algorithm::AesCbc(iv.as_bytes().to_vec()))
Ok(Algorithm::AesCbc(iv))
},
"AES-CTR" => {
let counter = algorithm
.get_optional::<_, ObjectBytes>("counter")?
.ok_or_else(|| {
Exception::throw_type(ctx, "algorithm 'counter' property required")
})?;
.ok_or_else(|| Exception::throw_type(ctx, "algorithm 'counter' property required"))?
.into_bytes();

let length = algorithm.get_optional("length")?.ok_or_else(|| {
Exception::throw_type(ctx, "algorithm 'length' property required")
})?;

Ok(Algorithm::AesCtr(counter.as_bytes().to_vec(), length))
Ok(Algorithm::AesCtr(counter, length))
},
"RSA-OAEP" => {
let label = algorithm.get_optional::<_, ObjectBytes>("label")?;
let label = label.map(|lbl| lbl.as_bytes().to_vec());
let label = label.map(|lbl| lbl.into_bytes());

Ok(Algorithm::RsaOaep(label))
},
Expand Down

0 comments on commit 7b736a1

Please sign in to comment.