pub enum Secret {
Raw(Vec<u8>),
Encoded(String),
}
Expand description
Shared secret between client and server to validate token against/generate token from.
Variants§
Implementations§
Source§impl Secret
impl Secret
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, SecretParseError>
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
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}
Sourcepub fn to_raw(&self) -> Result<Self, SecretParseError>
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
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}
Sourcepub fn to_encoded(&self) -> Self
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
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}
Sourcepub fn generate_secret() -> Secret
Available on crate feature gen_secret
only.
pub fn generate_secret() -> Secret
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 Error for Secret
impl Error for Secret
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§impl PartialEq for Secret
impl PartialEq for Secret
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more