feat: v1.11.14, add convert-pem-to-jwk

This commit is contained in:
2025-03-29 17:09:13 +08:00
parent e6409174b6
commit fe30f538ba
6 changed files with 76 additions and 4 deletions

33
src/ecutil.rs Normal file
View File

@@ -0,0 +1,33 @@
use crate::util::base64_decode;
use rust_util::XResult;
use spki::DecodePublicKey;
pub fn convert_ec_public_key_to_jwk(public_key: &str) -> XResult<String> {
if let Ok(jwk) = convert_ec_public_key_p256_to_jwk(public_key) {
return Ok(jwk);
}
if let Ok(jwk) = convert_ec_public_key_p384_to_jwk(public_key) {
return Ok(jwk);
}
simple_error!("Parse public key failed, MUST be pem or base64 encoded DER.")
}
pub fn convert_ec_public_key_p256_to_jwk(public_key: &str) -> XResult<String> {
let public_key_p256 = if public_key.contains("BEGIN PUBLIC KEY") {
p256::PublicKey::from_public_key_pem(public_key)?
} else {
let der = base64_decode(public_key)?;
p256::PublicKey::from_public_key_der(&der)?
};
Ok(public_key_p256.to_jwk_string())
}
pub fn convert_ec_public_key_p384_to_jwk(public_key: &str) -> XResult<String> {
let public_key_p384 = if public_key.contains("BEGIN PUBLIC KEY") {
p384::PublicKey::from_public_key_pem(public_key)?
} else {
let der = base64_decode(public_key)?;
p384::PublicKey::from_public_key_der(&der)?
};
Ok(public_key_p384.to_jwk_string())
}