Ob

Struct Ob 

Source
pub struct Ob { /* private fields */ }
Expand description

A flexible ObtextCodec implementation with runtime format selection.

Ob allows you to specify any format at runtime via constructor parameters, and provides methods to change the format after construction if needed.

This provides a unified interface for all runtime format needs, from immutable configurations to dynamic format switching.

§Examples

§Basic usage with immutable format

let ob = Ob::new("aasv.b64", &key)?;
let ot = ob.enc("hello")?; // obtext
let pt2 = ob.dec(&ot)?; // recovered plaintext
assert_eq!(pt2, "hello");

§Dynamic format switching

let mut ob = Ob::new("aasv.c32", &key)?;
let ot1 = ob.enc("hello")?; // aasv.c32 format

// Change format at runtime
ob.set_scheme(Scheme::Mock1)?;
let ot2 = ob.enc("hello")?; // mock1.c32 format

// Change encoding
ob.set_encoding(Encoding::B64)?; // now mock1.b64

// Set entire format at once
ob.set_format("aasv.hex")?; // now aasv.hex
ob.set_format(AASV_B64)?;   // now aasv.b64 (using constant)

Implementations§

Source§

impl Ob

Source

pub fn new(format: impl IntoFormat, key: &str) -> Result<Self, Error>

Create a new Ob with the specified format and base64 key.

Accepts either a format string (&str) or a Format instance.

§Examples
// Using format string
let ob1 = Ob::new("aasv.b64", &key)?;

// Using Format instance
let format = Format::new(Scheme::Aasv, Encoding::B64);
let ob2 = Ob::new(format, &key)?;
Source

pub fn set_format(&mut self, format: impl IntoFormat) -> Result<(), Error>

Set the format to a new value.

Accepts either a format string (&str) or a Format instance.

§Examples
let mut ob = Ob::new("aasv.c32", &key)?;
ob.set_format("mock1.b64")?; // switch using string
ob.set_format(Format::new(Scheme::Mock2, Encoding:: Hex))?; // switch using Format
Source

pub fn set_scheme(&mut self, scheme: Scheme) -> Result<(), Error>

Set the scheme while keeping the current encoding.

§Examples
let mut ob = Ob::new("aasv.c32", &key)?;
ob.set_scheme(Scheme::Mock1)?; // switch to mock1, keeping c32 encoding
Source

pub fn set_encoding(&mut self, encoding: Encoding) -> Result<(), Error>

Set the encoding while keeping the current scheme.

§Examples
let mut ob = Ob::new("aasv.c32", &key)?;
ob.set_encoding(Encoding::B64)?; // switch to b64, keeping aasv scheme
Source

pub fn autodec(&self, obtext: &str) -> Result<String, Error>

Decode and decrypt obtext with automatic format detection.

Tries to decode using the instance’s current encoding first (fast path), then falls back to full format autodetection if that fails.

§Examples
let mut ob = Ob::new("aasv.b64", &key)?;
let ot = ob.enc("test")?;

// Change scheme - autodec will still work
ob.set_scheme(oboron::Scheme::Mock1)?;
let pt2 = ob.autodec(&ot)?;
assert_eq!(pt2, "test");

// Works even with different encoding (slower fallback path)
ob.set_encoding(oboron::Encoding:: Hex)?;
let pt3 = ob.autodec(&ot)?; // Falls back to full autodetection
assert_eq!(pt3, "test");
Source

pub fn key(&self) -> String

Get the key as a base64 string.

§Examples
let ob = Ob::new("aasv.b64", &key)?;
let key_retrieved = ob.key();
assert_eq!(key_retrieved, key);
assert_eq!(key_retrieved.len(), 86); // 64 bytes = 86 base64 chars
Source§

impl Ob

Source

pub fn enc(&self, plaintext: &str) -> Result<String, Error>

Encrypt and encode plaintext to obtext.

§Examples
let ob = Ob::new("aasv.b64", &key)?;
let ot = ob.enc("secret data")?;
assert! (!ot.is_empty());
Source

pub fn dec(&self, obtext: &str) -> Result<String, Error>

Decode and decrypt obtext to plaintext.

Uses the instance’s configured format for decoding. Does not perform scheme autodetection - use autodec for that.

§Examples
let ob = Ob::new("aasv.b64", &key)?;
let ot = ob.enc("secret data")?;
let pt2 = ob.dec(&ot)?;
assert_eq!(pt2, "secret data");
Source

pub fn format(&self) -> Format

Get the current format (scheme + encoding).

§Examples
let ob = Ob::new("aasv.b64", &key)?;
let format = ob.format();
assert_eq!(format.scheme(), Scheme::Aasv);
assert_eq!(format.encoding(), Encoding::B64);
Source

pub fn scheme(&self) -> Scheme

Get the current scheme.

§Examples
let ob = Ob:: new("aasv.b64", &key)?;
assert_eq!(ob.scheme(), Scheme::Aasv);
Source

pub fn encoding(&self) -> Encoding

Get the current encoding.

§Examples
let ob = Ob:: new("aasv.b64", &key)?;
assert_eq!(ob.encoding(), Encoding::B64);

Trait Implementations§

Source§

impl ObtextCodec for Ob

Source§

fn enc(&self, plaintext: &str) -> Result<String, Error>

Encode a plaintext string.
Source§

fn dec(&self, obtext: &str) -> Result<String, Error>

Decode an encoded string back to plaintext
Source§

fn format(&self) -> Format

Get the full format (encapsulating scheme + encoding) used by this instance
Source§

fn scheme(&self) -> Scheme

Get the scheme identifier.
Source§

fn encoding(&self) -> Encoding

Get the encoding used by this instance.

Auto Trait Implementations§

§

impl Freeze for Ob

§

impl RefUnwindSafe for Ob

§

impl Send for Ob

§

impl Sync for Ob

§

impl Unpin for Ob

§

impl UnwindSafe for Ob

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V