Skip to main content

Padded

Trait Padded 

Source
pub trait Padded: ElGamalEncryptable {
    // Provided methods
    fn from_bytes_padded(data: &[u8]) -> Result<Self, Error>
       where Self: Sized { ... }
    fn from_string_padded(text: &str) -> Result<Self, Error>
       where Self: Sized { ... }
    fn to_string_padded(&self) -> Result<String, Error> { ... }
    fn to_bytes_padded(&self) -> Result<Vec<u8>, Error> { ... }
}
Expand description

A trait for encryptable types that support PKCS#7 padding for single-block (16 byte) encoding.

This trait provides methods to encode data up to 15 bytes using PKCS#7 padding, which fills the remaining bytes of a 16-byte block with padding bytes.

§Padding Format

  • For n bytes of data (0 ≤ n ≤ 15), add 16 - n padding bytes
  • Each padding byte has the value 16 - n
  • This allows unambiguous removal of padding during decoding

§Examples

use libpep::data::padding::Padded;
use libpep::data::simple::Attribute;

// Encode a string
let attr = Attribute::from_string_padded("hello")?;
let decoded = attr.to_string_padded()?;
assert_eq!(decoded, "hello");

// Encode bytes
let attr = Attribute::from_bytes_padded(b"data")?;
let decoded = attr.to_bytes_padded()?;
assert_eq!(decoded, b"data");

Provided Methods§

Source

fn from_bytes_padded(data: &[u8]) -> Result<Self, Error>
where Self: Sized,

Encodes an arbitrary byte array using PKCS#7 padding.

§Parameters
  • data: The bytes to encode (must be at most 15 bytes)
§Errors

Returns an error if the data exceeds 15 bytes.

Source

fn from_string_padded(text: &str) -> Result<Self, Error>
where Self: Sized,

Encodes a string using PKCS#7 padding.

§Parameters
  • text: The string to encode (must be at most 15 bytes when UTF-8 encoded)
§Errors

Returns an error if the string exceeds 15 bytes.

Source

fn to_string_padded(&self) -> Result<String, Error>

Decodes back to the original string.

§Errors

Returns an error if:

  • The padding is invalid
  • The decoded bytes are not valid UTF-8
  • The value was not created using from_bytes_padded or from_string_padded
Source

fn to_bytes_padded(&self) -> Result<Vec<u8>, Error>

Decodes back to the original byte array.

§Errors

Returns an error if:

  • The padding is invalid
  • The value was not created using from_bytes_padded or from_string_padded

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§