1
0
mirror of https://github.com/XFox111/SimpleOTP.git synced 2026-04-22 08:00:45 +03:00
1
simpleotp.otp
Eugene Fox edited this page 2024-09-18 16:16:09 +03:00

Otp

Namespace: SimpleOTP

Represents an abstract class for generating and validating One-Time Passwords (OTP).

public abstract class Otp

Inheritance ObjectOtp
Attributes NullableContextAttribute, NullableAttribute

Properties

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

Otp(OtpSecret, OtpAlgorithm, Int32)

Initializes a new instance of the Otp class.

public Otp(OtpSecret secret, OtpAlgorithm algorithm, int digits)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

algorithm OtpAlgorithm
The algorithm used for generating OTP codes.

digits Int32
The number of digits in the OTP code.

Otp(OtpSecret, OtpAlgorithm)

Initializes a new instance of the Otp class.

public Otp(OtpSecret secret, OtpAlgorithm algorithm)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

algorithm OtpAlgorithm
The algorithm used for generating OTP codes.

Otp(OtpSecret, Int32)

Initializes a new instance of the Otp class.

public Otp(OtpSecret secret, int digits)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

digits Int32
The number of digits in the OTP code.

Otp(OtpSecret)

Initializes a new instance of the Otp class.

public Otp(OtpSecret secret)

Parameters

secret OtpSecret
The secret key used for generating OTP codes.

Methods

Generate()

Generates an OTP code.

public OtpCode Generate()

Returns

OtpCode
The generated OTP code.

Generate(Int64)

Generates an OTP code for the specified counter value.

public OtpCode Generate(long counter)

Parameters

counter Int64
The counter value to generate the OTP code for.

Returns

OtpCode
The generated OTP code.

Validate(OtpCode)

Validates an OTP code.

public bool Validate(OtpCode code)

Parameters

code OtpCode
The OTP code to validate.

Returns

Boolean
true if the OTP code is valid; otherwise, false.

Exceptions

InvalidOperationException
Implementation for the Otp.Algorithm algorithm was not found. Use HashAlgorithmProviders.AddProvider(OtpAlgorithm, KeyedHashAlgorithm) to register an implementation.

Validate(OtpCode, ToleranceSpan)

Validates an OTP code with tolerance.

public bool Validate(OtpCode code, ToleranceSpan tolerance)

Parameters

code OtpCode
The OTP code to validate.

tolerance ToleranceSpan
The tolerance span for code validation.

Returns

Boolean
true if the OTP code is valid; otherwise, false.

Exceptions

InvalidOperationException
Implementation for the Otp.Algorithm algorithm was not found. Use HashAlgorithmProviders.AddProvider(OtpAlgorithm, KeyedHashAlgorithm) to register an implementation.

Validate(OtpCode, ToleranceSpan, Int32&)

Validates an OTP code with tolerance and returns the resynchronization value.

public bool Validate(OtpCode code, ToleranceSpan tolerance, Int32& resyncValue)

Parameters

code OtpCode
The OTP code to validate.

tolerance ToleranceSpan
The tolerance span for code validation.

resyncValue Int32&
The resynchronization value. Indicates how much given OTP code is ahead or behind the current counter value.

Returns

Boolean
true if the OTP code is valid; otherwise, false.

Exceptions

InvalidOperationException
Implementation for the Otp.Algorithm algorithm was not found. Use HashAlgorithmProviders.AddProvider(OtpAlgorithm, KeyedHashAlgorithm) to register an implementation.

Validate(OtpCode, ToleranceSpan, Int64, Int32&)

Validates an OTP code with tolerance and base counter value, and returns the resynchronization value.

public bool Validate(OtpCode code, ToleranceSpan tolerance, long baseCounter, Int32& resyncValue)

Parameters

code OtpCode
The OTP code to validate.

tolerance ToleranceSpan
The tolerance span for code validation.

baseCounter Int64
The base counter value.

resyncValue Int32&
The resynchronization value. Indicates how much given OTP code is ahead or behind the current counter value.

Returns

Boolean
true if the OTP code is valid; otherwise, false.

Exceptions

InvalidOperationException
Implementation for the Otp.Algorithm algorithm was not found. Use HashAlgorithmProviders.AddProvider(OtpAlgorithm, KeyedHashAlgorithm) to register an implementation.

GetCounter()

Gets the current counter value.

protected abstract long GetCounter()

Returns

Int64
The current counter value.

Compute(Int64)

Computes the OTP code for the specified counter value.

protected int Compute(long counter)

Parameters

counter Int64
The counter value to compute the OTP code for.

Returns

Int32
The OTP code for the specified counter value.

Exceptions

InvalidOperationException
Implementation for the Otp.Algorithm algorithm was not found. Use HashAlgorithmProviders.AddProvider(OtpAlgorithm, KeyedHashAlgorithm) to register an implementation.

Compute(Int64, KeyedHashAlgorithm)

Computes the OTP code for the specified counter value using provided hash algorithm.

protected int Compute(long counter, KeyedHashAlgorithm hashAlgorithm)

Parameters

counter Int64
The counter value to compute the OTP code for.

hashAlgorithm KeyedHashAlgorithm
The hash algorithm to use for computing the OTP code.

Returns

Int32
The OTP code for the specified counter value.

Remarks:

You need to dispose of the hashAlgorithm object yourself when you are done using it.