An R6 class that implements the Time based One Time Password (TOTP) algorithm.

Initialization


TOTP$new(secret, digits = 6L, period = 30, algorithm = "sha1")

Create an One Time Password object

  • secret a scalar character, the base32-based secret key.

  • digits an integer, the number of digits of the password.

  • period a positive number, the number of seconds in a time step.

  • algorithm the hash algorithm used, possible values are "sha1", "sha256" and "sha512".

Methods


TOTP$at_time(t)

Generate an one time password at a given time value.

  • t a POSIXct object or an integer that represents the numbers of second since UNIX epoch.


HOTP$verify(code, t, behind = 0L)

Verify if a given one time password is valid. Returns the beginning time of the time step window if there is a match within the behind window. Otherwise return NULL.

  • code a string of digits.

  • t a POSIXct object or an integer that represents the number of seconds since UNIX epoch.

  • behind a non-negative integer, the amount of time steps to look behind. A value of 1 means to accept the code before period seconds ago.


HOTP$provisioning_uri(name, issuer = NULL)

Return a provisioning uri which is compatible with google authenticator format.

  • name account name.

  • issuer issuer name.

Examples

p <- TOTP$new("JBSWY3DPEHPK3PXP")
(code <- p$now())
#> [1] "589341"
p$verify(code, behind = 1)
#> [1] "2024-01-22 15:56:00 PST"

(current_time <- Sys.time())
#> [1] "2024-01-22 15:56:23 PST"
(code <- p$at_time(current_time))
#> [1] "589341"
p$verify(code, current_time + 30, behind = 1)
#> [1] "2024-01-22 15:56:00 PST"

p$provisioning_uri("Alice", issuer = "example.com")
#> [1] "otpauth://totp/example.com:Alice?secret=JBSWY3DPEHPK3PXP&issuer=example.com"