@@ -25,51 +25,50 @@ fn get_otp_with_hotp() {
2525 let secret = " secret" ;
2626 let counter = 0 ;
2727 // 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 );
3131 println! (" The otp from hotp_str: {}" , otp_from_str );
32-
32+
3333 // 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 );
3737 println! (" The otp from hotp_bytes: {}" , otp_from_bytes );
38- }
38+ }
3939```
4040
4141To use TOTP:
4242
4343``` rust
4444use 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
4746use std :: time :: {Duration , SystemTime , UNIX_EPOCH };
4847
4948fn get_otp_with_totp () {
5049 let secret = " secret" ;
5150 let elapsed_seconds = SystemTime :: now ()
52- . duration_since (SystemTime :: UNIX_EPOCH )
51+ . duration_since (UNIX_EPOCH )
5352 . expect (" Error getting time" )
5453 . 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 );
5958 println! (" The otp from totp_sha1_str: {}" , otp_sha1 );
6059
61- // Alternatively get a TOTP instance with a '&[u8]' secret
60+ // Alternatively get a TOTP instance with an '&[u8]' secret
6261 // and different digest (Sha256 or Sha512)
63- let totp_sha256_bytes = TOTP :: new_with_digest (
62+ let totp_sha256_bytes = TOTP :: new (
6463 secret . as_bytes (),
65- MacDigest :: SHA256
64+ MacDigest :: SHA256 , // SHA256 algorithm
65+ 8 , // 8 digits
66+ 60 // 60-second interval
6667 );
6768 // 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 (
6970 elapsed_seconds ,
70- 30 , // A 60-second time step
7171 0 , // Start time at unix epoch
72- 6 // 8-digit code
7372 );
7473 println! (" The otp from totp_sha256_bytes: {}" , otp_sha256 );
7574}
0 commit comments