跳到主要内容

KeyPair

搜索

结构体 KeyPair 

Source
pub struct KeyPair { /* private fields */ }
展开描述

用于对证书和 CSR 进行签名的密钥对

实现§

Source§

impl KeyPair

Source

pub fn generate() -> Result<Self, Error>

生成一个新的随机 PKCS_ECDSA_P256_SHA256 密钥对

Source

pub fn generate_for(alg: &'static SignatureAlgorithm) -> Result<Self, Error>

为指定的签名算法生成一个新的随机密钥对

若你不确定该使用哪种算法,PKCS_ECDSA_P256_SHA256 是一个不错的选择。 若传入的是一种 RSA 签名算法,则取决于后端实现:可能返回生成的密钥, 也可能因不支持密钥生成而返回错误。目前只有 aws-lc-rs 支持 RSA 密钥生成。

Source

pub fn algorithm(&self) -> &'static SignatureAlgorithm

返回该密钥对的签名算法

Source

pub fn from_pem(pem_str: &str) -> Result<Self, Error>

从 ASCII PEM 格式解析密钥对

若使用了 aws_lc_rs feature,则该密钥必须是 DER 编码的明文私钥; 规定见 PKCS #8 / RFC 5958、SEC1 / RFC 5915 或 PKCS#1 / RFC 3447; 在 PEM 文件中以 “PRIVATE KEY”、“RSA PRIVATE KEY” 或 “EC PRIVATE KEY” 形式出现。

若使用的是 ring feature,则该密钥必须是 DER 编码的明文私钥; 规定见 PKCS #8 / RFC 5958;在 PEM 文件中以 “PRIVATE KEY” 形式出现。

Source

pub fn from_pkcs8_pem_and_sign_algo( pem_str: &str, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>

从 DER 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm

密钥必须是 DER 编码的明文私钥;规定见 PKCS #8 / RFC 5958;

在 PEM 文件中以 “PRIVATE KEY” 形式出现。 同 from_pkcs8_pem_and_sign_algo

Source

pub fn from_pkcs8_der_and_sign_algo( pkcs8: &PrivatePkcs8KeyDer<'_>, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>

从 DER 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm

若你拥有一个 PrivatePkcs8KeyDer,通常可以依赖 TryFrom 实现来得到一个 KeyPair—— 它会为你确定正确的 SignatureAlgorithm。 不过,有时同一段 DER 密钥可能匹配多种签名算法。这种情况下,你可以使用本函数精确地指定 SignatureAlgorithm

rustls_pemfile::private_key() 常用于从 PEM 输入得到 PrivateKeyDer。 若得到的 PrivateKeyDerPkcs8 变体,你可以将它的内容作为本函数的输入。 另外,若你已经有一段包含 DER 数据的字节切片,使用 Into trait 便可轻松地将其转换为 PrivatePkcs8KeyDer

Source

pub fn from_pem_and_sign_algo( pem_str: &str, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>

从 PEM 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm

若使用了 aws_lc_rs feature,则该密钥必须是 DER 编码的明文私钥; 规定见 PKCS #8 / RFC 5958、SEC1 / RFC 5915 或 PKCS#1 / RFC 3447; 在 PEM 文件中以 “PRIVATE KEY”、“RSA PRIVATE KEY” 或 “EC PRIVATE KEY” 形式出现。

若使用的是 ring feature,则该密钥必须是 DER 编码的明文私钥; 规定见 PKCS #8 / RFC 5958;在 PEM 文件中以 “PRIVATE KEY” 形式出现。

from_pem_and_sign_algo

Source

pub fn from_der_and_sign_algo( key: &PrivateKeyDer<'_>, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>

从 DER 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm

请注意,若使用 ring feature,本函数仅支持 PrivateKeyDer::Pkcs8 变体。 若需要完整支持 PrivateKeyDer,请考虑启用 aws_lc_rs feature。

如果你有 PrivateKeyDer,通常可依赖 TryFrom 实现来获取 KeyPair —— 它会为你确定正确的 SignatureAlgorithm。但有时同一 DER 密钥可能对应多个签名算法,此时你可以使用此函数精确指定 SignatureAlgorithm

你可以使用 rustls_pemfile::private_key 来获取密钥输入。 若你已有字节切片,只需调用 try_into() 即可将其转换为 PrivateKeyDer

Source

pub fn public_key_raw(&self) -> &[u8]

获取此密钥对的原始公钥

密钥为原始格式,与 KeyPair::public_key() 的输出以及 UnparsedPublicKey::verify() 所接受的格式一致。

Source

pub fn is_compatible(&self, signature_algorithm: &SignatureAlgorithm) -> bool

检查此密钥对是否可用于给定的签名算法

Source

pub fn compatible_algs( &self, ) -> impl Iterator<Item = &'static SignatureAlgorithm>

返回(可能多个)此密钥可兼容使用的 SignatureAlgorithm

Source

pub fn public_key_pem(&self) -> String

以 PEM 格式返回该密钥对的公钥

返回的字符串可通过 openssl pkey --inform PEM -pubout -pubin -text 进行解析

Source

pub fn serialize_der(&self) -> Vec<u8>

将该密钥对(包括私钥)以 PKCS#8 DER 格式序列化

Source

pub fn serialized_der(&self) -> &[u8]

返回序列化的密钥对(含私钥)的引用,格式为 PKCS#8 的 DER 编码

Source

pub fn serialize_pem(&self) -> String

将该密钥对(包括私钥)以 PKCS#8 PEM 格式序列化

Trait 实现§

Source§

impl Debug for KeyPair

Available on crate feature crypto only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息
Source§

impl From<KeyPair> for PrivateKeyDer<'static>

Available on crate feature crypto only.
Source§

fn from(val: KeyPair) -> Self

从输入类型转换为此类型。
Source§

impl From<KeyPair> for PrivatePkcs8KeyDer<'static>

Available on crate feature crypto only.
Source§

fn from(val: KeyPair) -> Self

从输入类型转换为此类型。
Source§

impl PublicKeyData for KeyPair

Available on crate feature crypto only.
Source§

fn der_bytes(&self) -> &[u8]

DER 格式的公钥
Source§

fn algorithm(&self) -> &'static SignatureAlgorithm

该密钥对所使用的算法
Source§

fn subject_public_key_info(&self) -> Vec<u8>

The public key data in DER format 更多信息
Source§

impl SigningKey for KeyPair

Available on crate feature crypto only.
Source§

fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, Error>

使用所选算法对 msg 进行签名
Source§

impl TryFrom<&[u8]> for KeyPair

Available on crate feature crypto only.
Source§

type Error = Error

转换出错时返回的类型。
Source§

fn try_from(key: &[u8]) -> Result<KeyPair, Error>

执行转换。
Source§

impl TryFrom<&PrivateKeyDer<'_>> for KeyPair

Available on crate feature crypto only.
Source§

type Error = Error

转换出错时返回的类型。
Source§

fn try_from(key: &PrivateKeyDer<'_>) -> Result<KeyPair, Error>

执行转换。
Source§

impl TryFrom<&PrivatePkcs8KeyDer<'_>> for KeyPair

Available on crate feature crypto only.
Source§

type Error = Error

转换出错时返回的类型。
Source§

fn try_from(key: &PrivatePkcs8KeyDer<'_>) -> Result<KeyPair, Error>

执行转换。
Source§

impl TryFrom<Vec<u8>> for KeyPair

Available on crate feature crypto only.
Source§

type Error = Error

转换出错时返回的类型。
Source§

fn try_from(key: Vec<u8>) -> Result<KeyPair, Error>

执行转换。

自动 Trait 实现§

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

调用 U::from(self)

That is, this conversion is whatever the implementation of From<T> for U 的实现方式。

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。