Skip to content

simpleotp.totp

Eugene Fox edited this page Sep 18, 2024 · 1 revision

Totp

Namespace: SimpleOTP

Represents a Time-based One-Time Password (TOTP) generator.

public class Totp : Otp

Inheritance ObjectOtpTotp
Attributes NullableContextAttribute, NullableAttribute

Properties

Period

Gets or sets the time period (in seconds) for which each generated OTP is valid.

public int Period { get; set; }

Property Value

Int32

Remarks:

Also used to calculate the current counter value.

Secret

Gets or sets the secret key used for generating OTPs.

public OtpSecret Secret { get; set; }

Property Value

OtpSecret

Algorithm

Gets or sets the algorithm used for generating OTP codes.

public OtpAlgorithm Algorithm { get; set; }

Property Value

OtpAlgorithm

Digits

Gets or sets the number of digits in the OTP code.

public int Digits { get; set; }

Property Value

Int32
Default: 6. Recommended: 6-8.

Constructors

Totp(OtpSecret)

Initializes a new instance of the Totp class with the specified secret key.

public Totp(OtpSecret secret)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

Totp(OtpSecret, Int32)

Initializes a new instance of the Totp class with the specified secret key and number of OTP code digits.

public Totp(OtpSecret secret, int period)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

period Int32
The time period (in seconds) for which each generated OTP is valid.

Totp(OtpSecret, Int32, Int32)

Initializes a new instance of the Totp class with the specified secret key, number of OTP code digits, and time period.

public Totp(OtpSecret secret, int period, int digits)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

period Int32
The time period (in seconds) for which each generated OTP is valid.

digits Int32
The number of digits in the OTP code.

Totp(OtpSecret, Int32, OtpAlgorithm)

Initializes a new instance of the Totp class with the specified secret key, hash algorithm, and time period.

public Totp(OtpSecret secret, int period, OtpAlgorithm algorithm)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

period Int32
The time period (in seconds) for which each generated OTP is valid.

algorithm OtpAlgorithm
The algorithm used for generating OTP codes.

Totp(OtpSecret, Int32, OtpAlgorithm, Int32)

Initializes a new instance of the Totp class with the specified secret key, hash algorithm, number of digits, and time period.

public Totp(OtpSecret secret, int period, OtpAlgorithm algorithm, int digits)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

period Int32
The time period (in seconds) for which each generated OTP is valid.

algorithm OtpAlgorithm
The algorithm used for generating OTP codes.

digits Int32
The number of digits in the OTP code.

Methods

Generate(Int64)

Generates an OTP based on the specified counter value.

public OtpCode Generate(long counter)

Parameters

counter Int64
The counter value to use for OTP generation.

Returns

OtpCode
An instance of OtpCode representing the generated OTP.

Generate(DateTimeOffset)

Generates an OTP based on the specified date and time.

public OtpCode Generate(DateTimeOffset date)

Parameters

date DateTimeOffset
The date and time to use for OTP generation.

Returns

OtpCode
An instance of OtpCode representing the generated OTP.

GetCounter()

Gets the current counter value based on the current UTC time and the configured time period.

protected long GetCounter()

Returns

Int64
The current counter value.

Clone this wiki locally