#[non_exhaustive]pub struct PublicKey { /* private fields */ }Expand description
Public recipient key for FerroCrypt public-key encryption.
Today, public recipient keys are native X25519 public keys. A PublicKey
can be read from a public.key file, wrap raw 32-byte X25519 public
material, or be decoded from a Bech32 fcr1… recipient string. Every
constructor resolves its source immediately, so the value holds key
material and never re-reads where that material came from.
That single resolution point is what makes an identity check meaningful:
a fingerprint taken from a PublicKey describes the key that same value
will encrypt to. Verify the fingerprint out of band, then keep the value
itself through the encryption call — passing the path again instead would
re-read a file that may have changed in between.
Once constructed, a PublicKey can be:
- passed to
crate::Encryptor::with_public_keyorcrate::Encryptor::with_public_keys; - rendered as a Bech32
fcr1…recipient string withPublicKey::to_recipient_string; - fingerprinted with
PublicKey::fingerprint.
Cloning retains the resolved key material and suite; it does not read the source again. Checksums validate the encoding, not the key owner’s identity.
This type implements Clone but deliberately not Copy: future recipient
types may hold key material that is not Copy.
Implementations§
Source§impl PublicKey
impl PublicKey
Sourcepub fn from_key_file(path: impl AsRef<Path>) -> Result<Self, CryptoError>
pub fn from_key_file(path: impl AsRef<Path>) -> Result<Self, CryptoError>
Reads and parses a FerroCrypt public.key file under
KeyReadLimits::default.
The file is read exactly once, by this call. Every later method uses the key material resolved here, so a change to the file afterwards cannot alter what this value fingerprints as or encrypts to.
§Errors
Returns CryptoError::InputPath if the file does not exist, and
CryptoError::Io for other read failures. Returns
CryptoError::InvalidFormat or
CryptoError::RecipientStringCapExceeded if the file is not a valid
public.key file, CryptoError::UnsupportedVersion for a public
key from an unsupported keypair suite, and
CryptoError::UnsupportedKeyType for a valid public key of a key
type this build does not support.
Sourcepub fn from_key_file_with_limits(
path: impl AsRef<Path>,
limits: KeyReadLimits,
) -> Result<Self, CryptoError>
pub fn from_key_file_with_limits( path: impl AsRef<Path>, limits: KeyReadLimits, ) -> Result<Self, CryptoError>
Reads a FerroCrypt public.key file under caller-supplied limits.
Same single read as PublicKey::from_key_file, with limits
applied to the parse.
§Errors
Same as PublicKey::from_key_file.
Sourcepub fn from_x25519_bytes(bytes: [u8; 32]) -> Result<Self, CryptoError>
pub fn from_x25519_bytes(bytes: [u8; 32]) -> Result<Self, CryptoError>
Wraps raw 32-byte X25519 public-key material directly.
Assigns the current writer’s key-pair suite. Raw bytes carry no suite
marker; use PublicKey::from_recipient_string or
PublicKey::from_key_file when loading an existing encoded key so its
suite is preserved. This constructor cannot verify that a separately
stored private key belongs to a supported suite.
Rejects the all-zero point and any non-canonical encoding
structurally. Per FORMAT.md §2.4 the accepted material is a
32-byte little-endian integer strictly below the Curve25519
field prime; the X25519 math would accept an alias of a valid
key (for example with the top bit set), but the raw bytes are
bound into fingerprints, recipient strings, and the wrap-key
derivation, so an alias is rejected rather than normalized.
Other degenerate inputs are caught at the ECDH site by wrap /
unwrap’s shared-secret check.
§Errors
Returns CryptoError::InvalidFormat with
FormatDefect::MalformedPublicKey
if bytes is the all-zero X25519 public key or a non-canonical
encoding.
Sourcepub fn from_recipient_string(recipient: &str) -> Result<Self, CryptoError>
pub fn from_recipient_string(recipient: &str) -> Result<Self, CryptoError>
Decodes a canonical lowercase Bech32 fcr1… recipient string
into a PublicKey. The accepted grammar is the string produced by
PublicKey::to_recipient_string, which is also the content of a
public.key file without the optional trailing newline. Validates
HRP, BIP 173 checksum, internal SHA3-256 checksum, payload structural
fields, type-name grammar, and, for X25519 recipients,
type_name == "x25519" with exactly 32 bytes of non-zero,
canonically encoded key material (FORMAT.md §2.4 / §7).
The keypair suite recovered from the wire-version byte is
preserved on the resulting PublicKey. Re-encoding via
PublicKey::to_recipient_string uses the parsed suite, not
the current writer suite, so a recipient string round-trips
byte-identically as long as the original suite is still
supported by this build.
§Errors
Returns CryptoError::InvalidFormat with
FormatDefect::MalformedPublicKey for text that is not a
canonical recipient string and for a malformed payload (an
invalid payload type name reports
FormatDefect::MalformedTypeName instead),
CryptoError::UnsupportedVersion for a recipient string from
an unsupported keypair suite,
CryptoError::UnsupportedKeyType for a valid recipient string
of a key type this build does not support, and
CryptoError::RecipientStringCapExceeded when the input
exceeds the KeyReadLimits::default recipient-string cap.
Sourcepub fn from_recipient_string_with_limits(
recipient: &str,
limits: KeyReadLimits,
) -> Result<Self, CryptoError>
pub fn from_recipient_string_with_limits( recipient: &str, limits: KeyReadLimits, ) -> Result<Self, CryptoError>
Decodes a recipient string under caller-supplied length limits.
Same validation as PublicKey::from_recipient_string, with the
recipient-string cap taken from limits.
§Errors
Same as PublicKey::from_recipient_string.
Sourcepub fn fingerprint(&self) -> Result<String, CryptoError>
pub fn fingerprint(&self) -> Result<String, CryptoError>
Computes the public-key fingerprint.
Returns 64 lowercase hexadecimal characters: SHA3-256 over
type_name || 0x00 || key_material, using the "x25519" type
name. The type name and zero-byte delimiter distinguish hash inputs
for different recipient types. Matches the ferrocrypt fingerprint
subcommand. Verify this fingerprint through a trusted channel to
establish the recipient’s identity.
§Errors
Returns the same errors as PublicKey::to_x25519_bytes.
Sourcepub fn to_recipient_string(&self) -> Result<String, CryptoError>
pub fn to_recipient_string(&self) -> Result<String, CryptoError>
Encodes the key as the canonical lowercase Bech32 fcr1…
recipient string.
Re-encodes using the keypair suite the key was originally
constructed with (preserved on every PublicKey ingress path),
not the current writer suite. A PublicKey parsed from a
fcr1… string round-trips byte-identically; a PublicKey
built from raw bytes via PublicKey::from_x25519_bytes re-encodes
using the writer suite (the suite from_x25519_bytes pins).
§Errors
Returns the same errors as PublicKey::to_x25519_bytes, plus
CryptoError::InternalInvariant if canonical Bech32 encoding fails
for already-validated X25519 bytes.
Sourcepub fn to_x25519_bytes(&self) -> Result<[u8; 32], CryptoError>
pub fn to_x25519_bytes(&self) -> Result<[u8; 32], CryptoError>
Returns the raw 32-byte X25519 public-key material as an owned array.
§Errors
Reserved for a future recipient type that carries no X25519 material.
Every PublicKey this build can construct holds a validated X25519
key, so the call currently always succeeds.