pub struct PrivateKeyImport {
key_data: PrivateKeyData,
}
Expand description
The key data required when importing a secret key
Fields§
§key_data: PrivateKeyData
Implementations§
source§impl PrivateKeyImport
impl PrivateKeyImport
sourcepub fn new(key_type: KeyType, key_data: &[u8]) -> Result<Self, Error>
pub fn new(key_type: KeyType, key_data: &[u8]) -> Result<Self, Error>
Creates a new PrivateKeyImport
Accepts a KeyType
(all except KeyType::Generic
) and a bytes array representing a
matching PKCS#8 private key in ASN.1 DER-encoded format.
§Errors
Returns an crate::Error::Key
if
key_data
can not be deserialized to a respective private key format.- an RSA private key does not have prime P or prime Q.
- an RSA private key is shorter than
MIN_RSA_BIT_LENGTH
. key_type
is the unsupportedKeyType::Generic
.
§Examples
use ed25519_dalek::{pkcs8::EncodePrivateKey, SigningKey};
use nethsm::{KeyType, PrivateKeyImport};
use rand::rngs::OsRng;
let key_data = {
let mut csprng = OsRng;
let signing_key: SigningKey = SigningKey::generate(&mut csprng);
signing_key.to_pkcs8_der()?.as_bytes().to_vec()
};
assert!(PrivateKeyImport::new(KeyType::Curve25519, &key_data).is_ok());
sourcepub fn from_pkcs8_pem(key_type: KeyType, key_data: &str) -> Result<Self, Error>
pub fn from_pkcs8_pem(key_type: KeyType, key_data: &str) -> Result<Self, Error>
Creates a new PrivateKeyImport
Accepts a KeyType
(all except KeyType::Generic
) and a string slice representing a
matching PKCS#8 private key in PEM-encoded format.
§Errors
Returns an crate::Error::Key
if
key_data
can not be deserialized to a respective private key format.- an RSA private key does not have prime P or prime Q.
- an RSA private key is shorter than
MIN_RSA_BIT_LENGTH
. key_type
is the unsupportedKeyType::Generic
.
§Examples
use std::ops::Deref;
use ed25519_dalek::{pkcs8::spki::der::pem::LineEnding, pkcs8::EncodePrivateKey, SigningKey};
use nethsm::{KeyType, PrivateKeyImport};
use rand::rngs::OsRng;
let key_data = {
let mut csprng = OsRng;
let signing_key: SigningKey = SigningKey::generate(&mut csprng);
signing_key.to_pkcs8_pem(LineEnding::default())?
};
assert!(PrivateKeyImport::from_pkcs8_pem(KeyType::Curve25519, key_data.deref()).is_ok());
sourcepub fn from_rsa(
prime_p: Vec<u8>,
prime_q: Vec<u8>,
public_exponent: Vec<u8>,
) -> Self
pub fn from_rsa( prime_p: Vec<u8>, prime_q: Vec<u8>, public_exponent: Vec<u8>, ) -> Self
Create PrivateKeyImport
object from raw, private RSA key parts.
The function takes two primes (p and q) and the public exponent,
which usually is 65537 ([0x01, 0x00, 0x01]
).
§Examples
use nethsm::PrivateKeyImport;
let prime_p = vec![7];
let prime_q = vec![11];
let public_exponent = vec![1, 0, 1];
let _import = PrivateKeyImport::from_rsa(prime_p, prime_q, public_exponent);
sourcepub fn from_raw_bytes(
ec: KeyType,
bytes: impl AsRef<[u8]>,
) -> Result<Self, Error>
pub fn from_raw_bytes( ec: KeyType, bytes: impl AsRef<[u8]>, ) -> Result<Self, Error>
Create PrivateKeyImport
object from raw, private Elliptic Curve bytes.
The function takes two parameters:
- the type of elliptic curve,
- raw bytes in a curve-specific encoding
Elliptic curve keys require the bytes
to be zero-padded to be of correct size.
This function automatically applies padding accordingly.
§Examples
use nethsm::{KeyType, PrivateKeyImport};
let bytes = vec![0x00; 32];
let _import = PrivateKeyImport::from_raw_bytes(KeyType::Curve25519, bytes)?;
sourcepub fn key_type(&self) -> KeyType
pub fn key_type(&self) -> KeyType
Get the matching KeyType
for the data contained in the PrivateKeyImport