feat: v1.11.14, add convert-pem-to-jwk
This commit is contained in:
33
src/ecutil.rs
Normal file
33
src/ecutil.rs
Normal 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())
|
||||
}
|
||||
Reference in New Issue
Block a user