pub fn generate(attributes: Attributes, id: Option<u32>) -> Result<Id> {
    initialized()?;
    let mut key_attributes = psa_crypto_sys::psa_key_attributes_t::try_from(attributes)?;
    if let Some(id) = id {
        unsafe { psa_crypto_sys::psa_set_key_id(&mut key_attributes, id) };
    }
    let mut id = 0;
    Status::from(unsafe { psa_crypto_sys::psa_generate_key(&key_attributes, &mut id) })
        .to_result()?;
    Attributes::reset(&mut key_attributes);

    Ok(Id(id))
}
/// Generate a key or a key pair
///
/// `id` can be set to `None` when creating a volatile key. Setting the `id` to something will
/// override the `lifetime` field of the attributes to `Lifetime::Persistent`.
/// When generating a persistent key with a
/// specific ID, the `Id` structure can be created after reset with the `from_persistent_key_id`
/// constructor on `Id`.
/// The `Id` structure returned can be used for cryptographic operations using that key.
///
/// # Example
///
/// ```
/// use psa_crypto::operations::key_management;
/// use psa_crypto::types::key::{Attributes, Type, Lifetime, Policy, UsageFlags};
/// use psa_crypto::types::algorithm::{AsymmetricSignature, Hash};
///
/// # let mut usage_flags: UsageFlags = Default::default();
/// # usage_flags.set_sign_hash().set_verify_hash();
/// # let mut attributes = Attributes {
/// #     key_type: Type::RsaKeyPair,
/// #     bits: 1024,
/// #     lifetime: Lifetime::Volatile,
/// #     policy: Policy {
/// #         usage_flags,
/// #         permitted_algorithms: AsymmetricSignature::RsaPkcs1v15Sign {
/// #             hash_alg: Hash::Sha256.into(),
/// #         }.into(),
/// #     },
/// # };
///
/// psa_crypto::init().unwrap();
/// let _my_key = key_management::generate(attributes, None).unwrap();
/// ```
Generate a key or a key pair
`id` can be set to `None` when creating a volatile key.
Generate a key or a key pair ` id ` can be set to ` None ` when creating a volatile key .
pub fn generate(attributes: Attributes, id: Option<u32>) -> Result<Id> {
    initialized()?;
    let mut key_attributes = psa_crypto_sys::psa_key_attributes_t::try_from(attributes)?;
    if let Some(id) = id {
        unsafe { psa_crypto_sys::psa_set_key_id(&mut key_attributes, id) };
    }
    let mut id = 0;
    Status::from(unsafe { psa_crypto_sys::psa_generate_key(&key_attributes, &mut id) })
        .to_result()?;
    Attributes::reset(&mut key_attributes);
    Ok(Id(id))
}
pub fn generate ( attributes : Attributes , id : Option < u32 > ) -> Result < Id > { initialized ( ) ? ; let mut key_attributes = psa_crypto_sys :: psa_key_attributes_t :: try_from ( attributes ) ? ; if let Some ( id ) = id { unsafe { psa_crypto_sys :: psa_set_key_id ( & mut key_attributes , id ) } ; } let mut id = 0 ; Status :: from ( unsafe { psa_crypto_sys :: psa_generate_key ( & key_attributes , & mut id ) } ) . to_result ( ) ? ; Attributes :: reset ( & mut key_attributes ) ; Ok ( Id ( id ) ) }
Generate a key or a key pair
`id` can be set to `None` when creating a volatile key.
Generate a key or a key pair ` id ` can be set to ` None ` when creating a volatile key .
attributes
Attributes
id
Option<u32>
attributes
Attributes
id
Option<u32>
example
```
use psa_crypto::operations::key_management;
use psa_crypto::types::key::{Attributes, Type, Lifetime, Policy, UsageFlags};
use psa_crypto::types::algorithm::{AsymmetricSignature, Hash}.
` ` ` use psa_crypto :: operations :: key_management ; use psa_crypto :: types :: key :: { Attributes Type Lifetime Policy UsageFlags } ; use psa_crypto :: types :: algorithm :: { AsymmetricSignature Hash } .
let mut usage_flags: usageflags = default::default();
None
usage_flags.set_sign_hash().set_verify_hash();
None
let mut attributes = attributes {
None
key_type: type::rsakeypair,
None
bits: 1024,
None
lifetime: lifetime::volatile,
None
policy: policy {
None
usage_flags,
None
permitted_algorithms: asymmetricsignature::rsapkcs1v15sign {
None
hash_alg: hash::sha256.into(),
None
}.into(),
None
},
None
};
:init().unwrap();
let _my_key = key_management::generate(attributes, None).unwrap();
```
: init () . unwrap () ; let _my_key = key_management :: generate ( attributes None ) . unwrap () ; ` ` `