Enum Secret

Source
pub enum Secret {
    Raw(Vec<u8>),
    Encoded(String),
}
Expand description

Shared secret between client and server to validate token against/generate token from.

Variants§

§

Raw(Vec<u8>)

Non-encoded “raw” secret.

§

Encoded(String)

Base32 encoded secret.

Implementations§

Source§

impl Secret

Source

pub fn to_bytes(&self) -> Result<Vec<u8>, SecretParseError>

Get the inner String value as a Vec of bytes.

Examples found in repository?
examples/gen_secret.rs (line 13)
5fn main() {
6    let secret = Secret::generate_secret();
7
8    let totp = TOTP::new(
9        Algorithm::SHA1,
10        6,
11        1,
12        30,
13        secret.to_bytes().unwrap(),
14        None,
15        "account".to_string(),
16    )
17    .unwrap();
18
19    println!(
20        "secret raw: {} ; secret base32 {} ; code: {}",
21        secret,
22        secret.to_encoded(),
23        totp.generate_current().unwrap()
24    )
25}
More examples
Hide additional examples
examples/steam.rs (line 9)
6fn main() {
7    // create TOTP from base32 secret
8    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
9    let totp_b32 = TOTP::new_steam(secret_b32.to_bytes().unwrap(), "user-account".to_string());
10
11    println!(
12        "base32 {} ; raw {}",
13        secret_b32,
14        secret_b32.to_raw().unwrap()
15    );
16    println!(
17        "code from base32:\t{}",
18        totp_b32.generate_current().unwrap()
19    );
20}
examples/secret.rs (line 12)
4fn main() {
5    // create TOTP from base32 secret
6    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
7    let totp_b32 = TOTP::new(
8        Algorithm::SHA1,
9        6,
10        1,
11        30,
12        secret_b32.to_bytes().unwrap(),
13        Some("issuer".to_string()),
14        "user-account".to_string(),
15    )
16    .unwrap();
17
18    println!(
19        "base32 {} ; raw {}",
20        secret_b32,
21        secret_b32.to_raw().unwrap()
22    );
23    println!(
24        "code from base32:\t{}",
25        totp_b32.generate_current().unwrap()
26    );
27
28    // create TOTP from raw binary value
29    let secret = [
30        0x70, 0x6c, 0x61, 0x69, 0x6e, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2d, 0x73, 0x65,
31        0x63, 0x72, 0x65, 0x74, 0x2d, 0x31, 0x32, 0x33,
32    ];
33    let secret_raw = Secret::Raw(secret.to_vec());
34    let totp_raw = TOTP::new(
35        Algorithm::SHA1,
36        6,
37        1,
38        30,
39        secret_raw.to_bytes().unwrap(),
40        Some("issuer".to_string()),
41        "user-account".to_string(),
42    )
43    .unwrap();
44
45    println!("raw {} ; base32 {}", secret_raw, secret_raw.to_encoded());
46    println!(
47        "code from raw secret:\t{}",
48        totp_raw.generate_current().unwrap()
49    );
50}
Source

pub fn to_raw(&self) -> Result<Self, SecretParseError>

Try to transform a Secret::Encoded into a Secret::Raw

Examples found in repository?
examples/steam.rs (line 14)
6fn main() {
7    // create TOTP from base32 secret
8    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
9    let totp_b32 = TOTP::new_steam(secret_b32.to_bytes().unwrap(), "user-account".to_string());
10
11    println!(
12        "base32 {} ; raw {}",
13        secret_b32,
14        secret_b32.to_raw().unwrap()
15    );
16    println!(
17        "code from base32:\t{}",
18        totp_b32.generate_current().unwrap()
19    );
20}
More examples
Hide additional examples
examples/secret.rs (line 21)
4fn main() {
5    // create TOTP from base32 secret
6    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
7    let totp_b32 = TOTP::new(
8        Algorithm::SHA1,
9        6,
10        1,
11        30,
12        secret_b32.to_bytes().unwrap(),
13        Some("issuer".to_string()),
14        "user-account".to_string(),
15    )
16    .unwrap();
17
18    println!(
19        "base32 {} ; raw {}",
20        secret_b32,
21        secret_b32.to_raw().unwrap()
22    );
23    println!(
24        "code from base32:\t{}",
25        totp_b32.generate_current().unwrap()
26    );
27
28    // create TOTP from raw binary value
29    let secret = [
30        0x70, 0x6c, 0x61, 0x69, 0x6e, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2d, 0x73, 0x65,
31        0x63, 0x72, 0x65, 0x74, 0x2d, 0x31, 0x32, 0x33,
32    ];
33    let secret_raw = Secret::Raw(secret.to_vec());
34    let totp_raw = TOTP::new(
35        Algorithm::SHA1,
36        6,
37        1,
38        30,
39        secret_raw.to_bytes().unwrap(),
40        Some("issuer".to_string()),
41        "user-account".to_string(),
42    )
43    .unwrap();
44
45    println!("raw {} ; base32 {}", secret_raw, secret_raw.to_encoded());
46    println!(
47        "code from raw secret:\t{}",
48        totp_raw.generate_current().unwrap()
49    );
50}
Source

pub fn to_encoded(&self) -> Self

Try to transforms a Secret::Raw into a Secret::Encoded.

Examples found in repository?
examples/gen_secret.rs (line 22)
5fn main() {
6    let secret = Secret::generate_secret();
7
8    let totp = TOTP::new(
9        Algorithm::SHA1,
10        6,
11        1,
12        30,
13        secret.to_bytes().unwrap(),
14        None,
15        "account".to_string(),
16    )
17    .unwrap();
18
19    println!(
20        "secret raw: {} ; secret base32 {} ; code: {}",
21        secret,
22        secret.to_encoded(),
23        totp.generate_current().unwrap()
24    )
25}
More examples
Hide additional examples
examples/secret.rs (line 45)
4fn main() {
5    // create TOTP from base32 secret
6    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
7    let totp_b32 = TOTP::new(
8        Algorithm::SHA1,
9        6,
10        1,
11        30,
12        secret_b32.to_bytes().unwrap(),
13        Some("issuer".to_string()),
14        "user-account".to_string(),
15    )
16    .unwrap();
17
18    println!(
19        "base32 {} ; raw {}",
20        secret_b32,
21        secret_b32.to_raw().unwrap()
22    );
23    println!(
24        "code from base32:\t{}",
25        totp_b32.generate_current().unwrap()
26    );
27
28    // create TOTP from raw binary value
29    let secret = [
30        0x70, 0x6c, 0x61, 0x69, 0x6e, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2d, 0x73, 0x65,
31        0x63, 0x72, 0x65, 0x74, 0x2d, 0x31, 0x32, 0x33,
32    ];
33    let secret_raw = Secret::Raw(secret.to_vec());
34    let totp_raw = TOTP::new(
35        Algorithm::SHA1,
36        6,
37        1,
38        30,
39        secret_raw.to_bytes().unwrap(),
40        Some("issuer".to_string()),
41        "user-account".to_string(),
42    )
43    .unwrap();
44
45    println!("raw {} ; base32 {}", secret_raw, secret_raw.to_encoded());
46    println!(
47        "code from raw secret:\t{}",
48        totp_raw.generate_current().unwrap()
49    );
50}
Source

pub fn generate_secret() -> Secret

Available on crate feature gen_secret only.

Generate a CSPRNG binary value of 160 bits, the recomended size from rfc-4226.

The length of the shared secret MUST be at least 128 bits. This document RECOMMENDs a shared secret length of 160 bits.

⚠️ The generated secret is not guaranteed to be a valid UTF-8 sequence.

Examples found in repository?
examples/gen_secret.rs (line 6)
5fn main() {
6    let secret = Secret::generate_secret();
7
8    let totp = TOTP::new(
9        Algorithm::SHA1,
10        6,
11        1,
12        30,
13        secret.to_bytes().unwrap(),
14        None,
15        "account".to_string(),
16    )
17    .unwrap();
18
19    println!(
20        "secret raw: {} ; secret base32 {} ; code: {}",
21        secret,
22        secret.to_encoded(),
23        totp.generate_current().unwrap()
24    )
25}

Trait Implementations§

Source§

impl Clone for Secret

Source§

fn clone(&self) -> Secret

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Secret

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Secret

Available on crate feature gen_secret only.
Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Secret

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Secret

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Error for Secret

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for Secret

Source§

fn eq(&self, other: &Self) -> bool

Will check that to_bytes() returns the same. One secret can be Raw, and the other Encoded.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Zeroize for Secret

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 Eq for Secret

Auto Trait Implementations§

§

impl Freeze for Secret

§

impl RefUnwindSafe for Secret

§

impl Send for Secret

§

impl Sync for Secret

§

impl Unpin for Secret

§

impl UnwindSafe for Secret

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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> ErasedDestructor for T
where T: 'static,