pub fn from_pem(
        input: impl AsRef<[u8]>,
        hash: Option<HashAlgorithm>,
        mgf1_hash: Option<HashAlgorithm>,
        salt_len: Option<u8>,
    ) -> Result<Self, JoseError> {
        (|| -> anyhow::Result<Self> {
            let input = input.as_ref();
            let (alg, data) = util::parse_pem(input)?;

            let pkcs8_der_vec;
            let (pkcs8_der, hash, mgf1_hash, salt_len) = match alg.as_str() {
                "PRIVATE KEY" | "RSA-PSS PRIVATE KEY" => match Self::detect_pkcs8(&data, false) {
                    Some((hash2, mgf1_hash2, salt_len2)) => {
                        let hash = match hash {
                            Some(val) if val == hash2 => hash2,
                            Some(_) => bail!("The hash algorithm is mismatched: {}", hash2),
                            None => hash2,
                        };

                        let mgf1_hash = match mgf1_hash {
                            Some(val) if val == mgf1_hash2 => mgf1_hash2,
                            Some(_) => {
                                bail!("The MGF1 hash algorithm is mismatched: {}", mgf1_hash2)
                            }
                            None => hash2,
                        };

                        let salt_len = match salt_len {
                            Some(val) if val == salt_len2 => salt_len2,
                            Some(_) => bail!("The salt length is mismatched: {}", salt_len2),
                            None => salt_len2,
                        };

                        (data.as_ref(), hash, mgf1_hash, salt_len)
                    }
                    None => bail!("Invalid PEM contents."),
                },
                "RSA PRIVATE KEY" => {
                    let hash = match hash {
                        Some(val) => val,
                        None => bail!("The hash algorithm is required."),
                    };

                    let mgf1_hash = match mgf1_hash {
                        Some(val) => val,
                        None => bail!("The MGF1 hash algorithm is required."),
                    };

                    let salt_len = match salt_len {
                        Some(val) => val,
                        None => bail!("The salt length is required."),
                    };

                    pkcs8_der_vec = Self::to_pkcs8(input, false, hash, mgf1_hash, salt_len);
                    (pkcs8_der_vec.as_slice(), hash, mgf1_hash, salt_len)
                }
                alg => bail!("Inappropriate algorithm: {}", alg),
            };

            let private_key = PKey::private_key_from_der(pkcs8_der)?;
            let rsa = private_key.rsa()?;
            let key_len = rsa.size();

            Ok(RsaPssKeyPair {
                private_key,
                key_len,
                hash,
                mgf1_hash,
                salt_len,
                algorithm: None,
                key_id: None,
            })
        })()
        .map_err(|err| JoseError::InvalidKeyFormat(err))
    }
/// Create a RSA-PSS key pair from a private key of common or traditinal PEM format.
///
/// Common PEM format is a DER and base64 encoded PKCS#8 PrivateKeyInfo
/// that surrounded by "-----BEGIN/END PRIVATE KEY----".
///
/// Traditional PEM format is a DER and base64 encoded PKCS#8 PrivateKeyInfo or PKCS#1 RSAPrivateKey
/// that surrounded by "-----BEGIN/END RSA-PSS/RSA PRIVATE KEY----".
///
/// # Arguments
/// * `input` - A private key of common or traditinal PEM format.
/// * `hash` A hash algorithm for signing
/// * `mgf1_hash` A hash algorithm for MGF1
/// * `salt_len` A salt length
Create a RSA-PSS key pair from a private key of common or traditinal PEM format.
Common PEM format is a DER and base64 encoded PKCS#8 PrivateKeyInfo
that surrounded by "BEGIN/END PRIVATE KEY----".


Create a RSA - PSS key pair from a private key of common or traditinal PEM format . Common PEM format is a DER and base64 encoded PKCS#8 PrivateKeyInfo that surrounded by " BEGIN / END PRIVATE KEY ---- " .
pub fn from_pem(
        input: impl AsRef<[u8]>,
        hash: Option<HashAlgorithm>,
        mgf1_hash: Option<HashAlgorithm>,
        salt_len: Option<u8>,
    ) -> Result<Self, JoseError> {
        (|| -> anyhow::Result<Self> {
            let input = input.as_ref();
            let (alg, data) = util::parse_pem(input)?;
            let pkcs8_der_vec;
            let (pkcs8_der, hash, mgf1_hash, salt_len) = match alg.as_str() {
                "PRIVATE KEY" | "RSA-PSS PRIVATE KEY" => match Self::detect_pkcs8(&data, false) {
                    Some((hash2, mgf1_hash2, salt_len2)) => {
                        let hash = match hash {
                            Some(val) if val == hash2 => hash2,
                            Some(_) => bail!("The hash algorithm is mismatched: {}", hash2),
                            None => hash2,
                        };
                        let mgf1_hash = match mgf1_hash {
                            Some(val) if val == mgf1_hash2 => mgf1_hash2,
                            Some(_) => {
                                bail!("The MGF1 hash algorithm is mismatched: {}", mgf1_hash2)
                            }
                            None => hash2,
                        };
                        let salt_len = match salt_len {
                            Some(val) if val == salt_len2 => salt_len2,
                            Some(_) => bail!("The salt length is mismatched: {}", salt_len2),
                            None => salt_len2,
                        };
                        (data.as_ref(), hash, mgf1_hash, salt_len)
                    }
                    None => bail!("Invalid PEM contents."),
                },
                "RSA PRIVATE KEY" => {
                    let hash = match hash {
                        Some(val) => val,
                        None => bail!("The hash algorithm is required."),
                    };
                    let mgf1_hash = match mgf1_hash {
                        Some(val) => val,
                        None => bail!("The MGF1 hash algorithm is required."),
                    };
                    let salt_len = match salt_len {
                        Some(val) => val,
                        None => bail!("The salt length is required."),
                    };
                    pkcs8_der_vec = Self::to_pkcs8(input, false, hash, mgf1_hash, salt_len);
                    (pkcs8_der_vec.as_slice(), hash, mgf1_hash, salt_len)
                }
                alg => bail!("Inappropriate algorithm: {}", alg),
            };
            let private_key = PKey::private_key_from_der(pkcs8_der)?;
            let rsa = private_key.rsa()?;
            let key_len = rsa.size();
            Ok(RsaPssKeyPair {
                private_key,
                key_len,
                hash,
                mgf1_hash,
                salt_len,
                algorithm: None,
                key_id: None,
            })
        })()
        .map_err(|err| JoseError::InvalidKeyFormat(err))
    }
pub fn from_pem ( input : impl AsRef < [ u8 ] > , hash : Option < HashAlgorithm > , mgf1_hash : Option < HashAlgorithm > , salt_len : Option < u8 > , ) -> Result < Self , JoseError > { ( | | -> anyhow :: Result < Self > { let input = input . as_ref ( ) ; let ( alg , data ) = util :: parse_pem ( input ) ? ; let pkcs8_der_vec ; let ( pkcs8_der , hash , mgf1_hash , salt_len ) = match alg . as_str ( ) { " " | " " => match Self :: detect_pkcs8 ( & data , false ) { Some ( ( hash2 , mgf1_hash2 , salt_len2 ) ) => { let hash = match hash { Some ( val ) if val == hash2 => hash2 , Some ( _ ) => bail ! ( " " hash2 ) , None => hash2 , } ; let mgf1_hash = match mgf1_hash { Some ( val ) if val == mgf1_hash2 => mgf1_hash2 , Some ( _ ) => { bail ! ( " " mgf1_hash2 ) } None => hash2 , } ; let salt_len = match salt_len { Some ( val ) if val == salt_len2 => salt_len2 , Some ( _ ) => bail ! ( " " salt_len2 ) , None => salt_len2 , } ; ( data . as_ref ( ) , hash , mgf1_hash , salt_len ) } None => bail ! ( " " ) , } , " " => { let hash = match hash { Some ( val ) => val , None => bail ! ( " " ) , } ; let mgf1_hash = match mgf1_hash { Some ( val ) => val , None => bail ! ( " " ) , } ; let salt_len = match salt_len { Some ( val ) => val , None => bail ! ( " " ) , } ; pkcs8_der_vec = Self :: to_pkcs8 ( input , false , hash , mgf1_hash , salt_len ) ; ( pkcs8_der_vec . as_slice ( ) , hash , mgf1_hash , salt_len ) } alg => bail ! ( " " alg ) , } ; let private_key = PKey :: private_key_from_der ( pkcs8_der ) ? ; let rsa = private_key . rsa ( ) ? ; let key_len = rsa . size ( ) ; Ok ( RsaPssKeyPair { private_key , key_len , hash , mgf1_hash , salt_len , algorithm : None , key_id : None , } ) } ) ( ) . map_err ( | err | JoseError :: InvalidKeyFormat ( err ) ) }
Create a RSA-PSS key pair from a private key of common or traditinal PEM format.
Create a RSA - PSS key pair from a private key of common or traditinal PEM format .
input
impl AsRef<[u8]>
hash
Option<HashAlgorithm>
mgf1_hash
Option<HashAlgorithm>
salt_len
Option<u8>
input
impl AsRef<[u8]>
hash
Option<HashAlgorithm>
mgf1_hash
Option<HashAlgorithm>
salt_len
Option<u8>