totally

Types

One Time Password type

pub opaque type OTP

Algorithm used for the hash function

pub type TOTPAlgorithm {
  Sha1
  Sha256
  Sha512
}

Constructors

  • Sha1
  • Sha256
  • Sha512

Configuration for the TOTP

pub type TOTPConfig {
  TOTPConfig(
    secret: Secret,
    time: Int,
    period: Int,
    digits: Int,
    algorithm: TOTPAlgorithm,
    issuer: String,
    account: String,
  )
}

Constructors

  • TOTPConfig(
      secret: Secret,
      time: Int,
      period: Int,
      digits: Int,
      algorithm: TOTPAlgorithm,
      issuer: String,
      account: String,
    )

Functions

pub fn default_config() -> TOTPConfig

Creates a default configuration for TOTP with the following values: algorithm: Sha1, period: 30, digits: 6. These are the most commonly used TOTP settings. Please set the secret and time with the set_secret and set_time_now or manually set_time functions.

pub fn otp_to_string(otp: OTP) -> String

Converts the OTP to a string.

pub fn otpauth_uri(
  secret secret: BitArray,
  issuer issuer: String,
  account account_name: String,
) -> String

Generates an otpauth URI for the given secret, issuer and account name. The otpauth URI is used to generate QR codes for TOTP.

pub fn otpauth_uri_from_config(config: TOTPConfig) -> String

Generates an otpauth URI for the given TOTP configuration.

pub fn secret() -> BitArray

Generates a random 20 byte secret. 20 bytes is the recommended size according to the HOTP RFC4226 (https://tools.ietf.org/html/rfc4226#section-4).

pub fn secret_with_size(size: Int) -> BitArray

Generates a random secret with the given size.

pub fn set_account(
  config: TOTPConfig,
  account: String,
) -> TOTPConfig

Sets the account for the TOTP configuration.

pub fn set_algorithm(
  config: TOTPConfig,
  algorithm: TOTPAlgorithm,
) -> TOTPConfig

Sets the algorithm for the TOTP configuration. Most commonly used is Sha1.

pub fn set_digits(config: TOTPConfig, digits: Int) -> TOTPConfig

Sets the digits for the TOTP configuration. Most commonly used is 6 digits. The spec allows for 6 to 8 digits.

pub fn set_issuer(
  config: TOTPConfig,
  issuer: String,
) -> TOTPConfig

Sets the issuer for the TOTP configuration. Used for the otpauth URI.

pub fn set_period(config: TOTPConfig, period: Int) -> TOTPConfig

Sets the refresh period in seconds for the TOTP configuration. Most commonly used is 30 seconds.

pub fn set_secret(
  config: TOTPConfig,
  secret: BitArray,
) -> TOTPConfig

Sets the secret for the TOTP configuration.

pub fn set_time(config: TOTPConfig, time: Int) -> TOTPConfig

Sets the time in unix timestamp seconds for the TOTP configuration.

pub fn set_time_now(config: TOTPConfig) -> TOTPConfig

Sets the time for the TOTP configuration to the current time.

pub fn string_to_otp(otp: String) -> Result(OTP, String)

Converts a valid OTP string to an OTP type.

pub fn totp(secret: BitArray) -> OTP

Generates a TOTP using the given secret and default configuration.

pub fn totp_from_config(config: TOTPConfig) -> OTP

Generates a TOTP using the given TOTP configuration.

pub fn verify(
  secret secret: BitArray,
  input totp_input: String,
) -> Bool

Verifies the given TOTP input with the given secret.

pub fn verify_from_config(
  config: TOTPConfig,
  input totp_input: String,
) -> Bool

Verifies the given TOTP input with the given TOTP configuration.

Search Document