Skip to main content

TimeLockKey

Struct TimeLockKey 

Source
pub struct TimeLockKey(/* private fields */);
Expand description

A derived 32-byte time-locked key.

The inner bytes are automatically overwritten (ZeroizeOnDrop) the moment this value is dropped. Access the key via as_bytes.

Implementations§

Source§

impl TimeLockKey

Source

pub fn as_bytes(&self) -> &[u8; 32]

Borrow the raw 32-byte key.

The reference is valid only while this TimeLockKey is alive. If you must copy the bytes into another buffer, protect it with Zeroize too.

Examples found in repository?
examples/timelock_round_trip.rs (line 50)
24fn main() {
25    // ── Encryption side ───────────────────────────────────────────────────────
26    // Generate fresh salts.  Salts are NOT secret — store them in plaintext
27    // alongside the ciphertext so the decryption side can reproduce the key.
28    let salts = TimeLockSalts::generate();
29
30    // Use a deliberately fast preset so the example finishes quickly.
31    // In production use KdfPreset::Balanced or stronger.
32    let kdf = KdfPreset::Balanced.params();
33
34    // Lock to any Tuesday at 18:00 (hour-precision window = the full 18:00–18:59 block).
35    let cadence   = TimeLockCadence::DayOfWeek(Weekday::Tuesday);
36    let lock_time = TimeLockTime::new(18, 0).unwrap();
37
38    println!("Deriving encryption key (this may take a few seconds)…");
39    let enc_key = timelock(
40        Some(cadence.clone()),
41        Some(lock_time),
42        Some(TimePrecision::Hour),
43        Some(TimeFormat::Hour24),
44        Some(salts.clone()),
45        Some(kdf),
46        None,   // params = None → _at (encryption) path
47    )
48    .expect("encryption-side key derivation failed");
49
50    println!("enc_key[:8] = {:02x?}", &enc_key.as_bytes()[..8]);
51
52    // Pack every setting — including salts and KDF params — into a compact header.
53    // This header goes into the ciphertext in plaintext; nothing here is secret.
54    let header = pack(
55        TimePrecision::Hour,
56        TimeFormat::Hour24,
57        &cadence,
58        salts,
59        kdf,
60    );
61
62    // ── Decryption side ───────────────────────────────────────────────────────
63    // Load `header` from the ciphertext and call timelock() at the matching
64    // time slot with params = Some(header).
65    println!("Deriving decryption key from system clock…");
66    let dec_key = timelock(
67        None, None, None, None, None, None,
68        Some(header),   // params = Some → _now (decryption) path
69    )
70    .expect("decryption-side key derivation failed");
71
72    println!("dec_key[:8] = {:02x?}", &dec_key.as_bytes()[..8]);
73
74    // ── Verdict ───────────────────────────────────────────────────────────────
75    if enc_key.as_bytes() == dec_key.as_bytes() {
76        println!("\nKeys match ✓  — running on a Tuesday at 18:xx");
77    } else {
78        println!("\nKeys differ — not running on a Tuesday at 18:xx (expected outside that window)");
79    }
80}

Trait Implementations§

Source§

impl Drop for TimeLockKey

Available on crate features enc-timelock-keygen-now or enc-timelock-keygen-input or enc-timelock-async-keygen-now or enc-timelock-async-keygen-input only.
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Zeroize for TimeLockKey

Available on crate features enc-timelock-keygen-now or enc-timelock-keygen-input or enc-timelock-async-keygen-now or enc-timelock-async-keygen-input only.
Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
Source§

impl ZeroizeOnDrop for TimeLockKey

Available on crate features enc-timelock-keygen-now or enc-timelock-keygen-input or enc-timelock-async-keygen-now or enc-timelock-async-keygen-input only.

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more