@@ -25,51 +25,50 @@ fn get_otp_with_hotp() {
25
25
let secret = " secret" ;
26
26
let counter = 0 ;
27
27
// Get a HOTP instance with a '&str' secret
28
- let hotp_str = HOTP :: from_utf8 (secret );
29
- // Get an otp with the given counter and digit count
30
- let otp_from_str = hotp_str . get_otp (counter , 6 );
28
+ let hotp_str = HOTP :: default_from_utf8 (secret );
29
+ // Get an otp with the given counter
30
+ let otp_from_str = hotp_str . get_otp (counter );
31
31
println! (" The otp from hotp_str: {}" , otp_from_str );
32
-
32
+
33
33
// Alternatively, get a HOTP instance with a '&[u8]' secret
34
- let hotp_bytes = HOTP :: new (secret . as_bytes ());
35
- // Get an otp with the given counter and digit count
36
- let otp_from_bytes = hotp_bytes . get_otp (counter , 6 );
34
+ let hotp_bytes = HOTP :: new (secret . as_bytes (), 6 );
35
+ // Get an otp with the given counter
36
+ let otp_from_bytes = hotp_bytes . get_otp (counter );
37
37
println! (" The otp from hotp_bytes: {}" , otp_from_bytes );
38
- }
38
+ }
39
39
```
40
40
41
41
To use TOTP:
42
42
43
43
``` rust
44
44
use xotp :: totp :: TOTP ;
45
- use xotp :: util :: MacDigest ;
46
- // Only needed if using a non-SHA1 hash function
45
+ use xotp :: util :: MacDigest ; // Only needed if using a non-SHA1 hash function
47
46
use std :: time :: {Duration , SystemTime , UNIX_EPOCH };
48
47
49
48
fn get_otp_with_totp () {
50
49
let secret = " secret" ;
51
50
let elapsed_seconds = SystemTime :: now ()
52
- . duration_since (SystemTime :: UNIX_EPOCH )
51
+ . duration_since (UNIX_EPOCH )
53
52
. expect (" Error getting time" )
54
53
. as_secs ();
55
- // Get a TOTP instance a '&str' secret and default SHA1 Digest
56
- let totp_sha1_str = TOTP :: from_utf8 (secret );
57
- // Get an otp with the given counter and elapsed seconds
58
- let otp_sha1 = totp_sha1_str . get_otp (elapsed_seconds , 8 );
54
+ // Get a TOTP instance with an '&str' secret and default SHA1 Digest
55
+ let totp_sha1_str = TOTP :: default_from_utf8 (secret );
56
+ // Get an otp with the given counter and elapsed seconds
57
+ let otp_sha1 = totp_sha1_str . get_otp (elapsed_seconds );
59
58
println! (" The otp from totp_sha1_str: {}" , otp_sha1 );
60
59
61
- // Alternatively get a TOTP instance with a '&[u8]' secret
60
+ // Alternatively get a TOTP instance with an '&[u8]' secret
62
61
// and different digest (Sha256 or Sha512)
63
- let totp_sha256_bytes = TOTP :: new_with_digest (
62
+ let totp_sha256_bytes = TOTP :: new (
64
63
secret . as_bytes (),
65
- MacDigest :: SHA256
64
+ MacDigest :: SHA256 , // SHA256 algorithm
65
+ 8 , // 8 digits
66
+ 60 // 60-second interval
66
67
);
67
68
// Get an otp with the given counter, time and other custom params
68
- let otp_sha256 = totp_sha256_bytes . get_otp_with_custom (
69
+ let otp_sha256 = totp_sha256_bytes . get_otp_with_custom_time_start (
69
70
elapsed_seconds ,
70
- 30 , // A 60-second time step
71
71
0 , // Start time at unix epoch
72
- 6 // 8-digit code
73
72
);
74
73
println! (" The otp from totp_sha256_bytes: {}" , otp_sha256 );
75
74
}
0 commit comments