Enum totp_rs::Secret

source ·
pub enum Secret {
    Raw(Vec<u8>),
    Encoded(String),
}

Variants§

§

Raw(Vec<u8>)

represent a non-encoded “raw” secret

§

Encoded(String)

represent a 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)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let secret = Secret::generate_secret();

    let totp = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret.to_bytes().unwrap(),
        None,
        "account".to_string(),
    )
    .unwrap();

    println!(
        "secret raw: {} ; secret base32 {} ; code: {}",
        secret,
        secret.to_encoded(),
        totp.generate_current().unwrap()
    )
}
More examples
Hide additional examples
examples/secret.rs (line 12)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    // create TOTP from base32 secret
    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
    let totp_b32 = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret_b32.to_bytes().unwrap(),
        Some("issuer".to_string()),
        "user-account".to_string(),
    )
    .unwrap();

    println!(
        "base32 {} ; raw {}",
        secret_b32,
        secret_b32.to_raw().unwrap()
    );
    println!(
        "code from base32:\t{}",
        totp_b32.generate_current().unwrap()
    );

    // create TOTP from raw binary value
    let secret = [
        0x70, 0x6c, 0x61, 0x69, 0x6e, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2d, 0x73, 0x65,
        0x63, 0x72, 0x65, 0x74, 0x2d, 0x31, 0x32, 0x33,
    ];
    let secret_raw = Secret::Raw(secret.to_vec());
    let totp_raw = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret_raw.to_bytes().unwrap(),
        Some("issuer".to_string()),
        "user-account".to_string(),
    )
    .unwrap();

    println!("raw {} ; base32 {}", secret_raw, secret_raw.to_encoded());
    println!(
        "code from raw secret:\t{}",
        totp_raw.generate_current().unwrap()
    );
}
source

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

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

Examples found in repository?
examples/secret.rs (line 21)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    // create TOTP from base32 secret
    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
    let totp_b32 = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret_b32.to_bytes().unwrap(),
        Some("issuer".to_string()),
        "user-account".to_string(),
    )
    .unwrap();

    println!(
        "base32 {} ; raw {}",
        secret_b32,
        secret_b32.to_raw().unwrap()
    );
    println!(
        "code from base32:\t{}",
        totp_b32.generate_current().unwrap()
    );

    // create TOTP from raw binary value
    let secret = [
        0x70, 0x6c, 0x61, 0x69, 0x6e, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2d, 0x73, 0x65,
        0x63, 0x72, 0x65, 0x74, 0x2d, 0x31, 0x32, 0x33,
    ];
    let secret_raw = Secret::Raw(secret.to_vec());
    let totp_raw = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret_raw.to_bytes().unwrap(),
        Some("issuer".to_string()),
        "user-account".to_string(),
    )
    .unwrap();

    println!("raw {} ; base32 {}", secret_raw, secret_raw.to_encoded());
    println!(
        "code from raw secret:\t{}",
        totp_raw.generate_current().unwrap()
    );
}
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)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let secret = Secret::generate_secret();

    let totp = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret.to_bytes().unwrap(),
        None,
        "account".to_string(),
    )
    .unwrap();

    println!(
        "secret raw: {} ; secret base32 {} ; code: {}",
        secret,
        secret.to_encoded(),
        totp.generate_current().unwrap()
    )
}
More examples
Hide additional examples
examples/secret.rs (line 45)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    // create TOTP from base32 secret
    let secret_b32 = Secret::Encoded(String::from("OBWGC2LOFVZXI4TJNZTS243FMNZGK5BNGEZDG"));
    let totp_b32 = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret_b32.to_bytes().unwrap(),
        Some("issuer".to_string()),
        "user-account".to_string(),
    )
    .unwrap();

    println!(
        "base32 {} ; raw {}",
        secret_b32,
        secret_b32.to_raw().unwrap()
    );
    println!(
        "code from base32:\t{}",
        totp_b32.generate_current().unwrap()
    );

    // create TOTP from raw binary value
    let secret = [
        0x70, 0x6c, 0x61, 0x69, 0x6e, 0x2d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2d, 0x73, 0x65,
        0x63, 0x72, 0x65, 0x74, 0x2d, 0x31, 0x32, 0x33,
    ];
    let secret_raw = Secret::Raw(secret.to_vec());
    let totp_raw = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret_raw.to_bytes().unwrap(),
        Some("issuer".to_string()),
        "user-account".to_string(),
    )
    .unwrap();

    println!("raw {} ; base32 {}", secret_raw, secret_raw.to_encoded());
    println!(
        "code from raw secret:\t{}",
        totp_raw.generate_current().unwrap()
    );
}
source

pub fn generate_secret() -> Secret

⚠️ requires feature gen_secret

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)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let secret = Secret::generate_secret();

    let totp = TOTP::new(
        Algorithm::SHA1,
        6,
        1,
        30,
        secret.to_bytes().unwrap(),
        None,
        "account".to_string(),
    )
    .unwrap();

    println!(
        "secret raw: {} ; secret base32 {} ; code: {}",
        secret,
        secret.to_encoded(),
        totp.generate_current().unwrap()
    )
}

Trait Implementations§

source§

impl Clone for Secret

source§

fn clone(&self) -> Secret

Returns a copy 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

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

1.30.0 · source§

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

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, demand: &mut Demand<'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<Secret> 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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Secret

source§

impl StructuralEq for Secret

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<E> Provider for Ewhere E: Error + ?Sized,

source§

fn provide<'a>(&'a self, demand: &mut Demand<'a>)

🔬This is a nightly-only experimental API. (provide_any)
Data providers should implement this method to provide all values they are able to provide by using demand. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V