C# Class TwoFactorAuthNet.TwoFactorAuth

Provides methods to enable 2FA (Two Factor Authentication).
This library only provides the TOTP (Time-based One-time Password) implementation of 2FA. It does not provide a HOTP (HMAC-based One-time Password) implementation.
Show file Open project: RobThree/TwoFactorAuth.Net

Public Methods

Method Description
CreateSecret ( ) : string

Creates a 80 bit secret key to be shared with the user on wich the future valid TOTP codes will be based. The CryptoSecureRequirement is .

CreateSecret ( int bits ) : string

Creates a secret key with the specified number of bits of entropy to be shared with the user on wich the future valid TOTP codes will be based. The CryptoSecureRequirement is .

CreateSecret ( int bits, CryptoSecureRequirement cryptoSecureRequirement ) : string

Creates a secret key with the specified number of bits of entropy and specified CryptoSecureRequirement to be shared with the user on wich the future valid TOTP codes will be based.

GetCode ( string secret ) : string

Gets a TOTP code based on the specified secret for the current time.

GetCode ( string secret, System.DateTime dateTime ) : string

Gets a TOTP code based on the specified secret for the specified DateTime.

GetCode ( string secret, long timestamp ) : string

Gets a TOTP code based on the specified secret for the specified timestamp.

GetQrCodeImageAsDataUri ( string label, string secret ) : string

Retrieves / generates a QR code to be displayed to the user for sharing the shared secret and easy input of this code by scanning with a default size (DEFAULTQRCODESIZE).

GetQrCodeImageAsDataUri ( string label, string secret, int size ) : string

Retrieves / generates a QR code to be displayed to the user for sharing the shared secret and easy input of this code by scanning with a specified size.

TwoFactorAuth ( ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, Algorithm algorithm ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, Algorithm algorithm, IQrCodeProvider qrcodeprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, Algorithm algorithm, IQrCodeProvider qrcodeprovider, IRngProvider rngprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, Algorithm algorithm, IRngProvider rngprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, IQrCodeProvider qrcodeprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, IQrCodeProvider qrcodeprovider, IRngProvider rngprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, IRngProvider rngprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, int digits ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, int digits, int period ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, int digits, int period, Algorithm algorithm ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, int digits, int period, Algorithm algorithm, IQrCodeProvider qrcodeprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

TwoFactorAuth ( string issuer, int digits, int period, Algorithm algorithm, IQrCodeProvider qrcodeprovider, IRngProvider rngprovider ) : System

Intializes a new instance of the TwoFactorAuth class.

VerifyCode ( string secret, string code ) : bool

Verifies a TOTP code with the shared secret for the current time and with a DEFAULTDISCREPANCY.

VerifyCode ( string secret, string code, int discrepancy ) : bool

Verifies a TOTP code with the shared secret for the current time and with a specified discrepancy.

VerifyCode ( string secret, string code, int discrepancy, System.DateTime dateTime ) : bool

Verifies a TOTP code with the shared secret for the specified DateTime and with a specified discrepancy.

VerifyCode ( string secret, string code, int discrepancy, long timestamp ) : bool

Verifies a TOTP code with the shared secret for the specified timestamp and with a specified discrepancy.

Private Methods

Method Description
CodeEquals ( string safe, string user ) : bool

Provides a timing-attack safe method of comparing 2 strings.

DateTimeToTimestamp ( System.DateTime value ) : long

Converts a DateTime to timestamp (based on UNIX EPOCH, see EPOCH).

GetQrText ( string label, string secret ) : string

Generates a TOTP Uri with specified label and secret.

GetTimeSlice ( long timestamp, int offset ) : long

Calculates the timeslice (e.g. number of periods since EPOCH) for a given timestamp and offset (specified in number of periods).

Method Details

CreateSecret() public method

Creates a 80 bit secret key to be shared with the user on wich the future valid TOTP codes will be based. The CryptoSecureRequirement is .
public CreateSecret ( ) : string
return string

CreateSecret() public method

Creates a secret key with the specified number of bits of entropy to be shared with the user on wich the future valid TOTP codes will be based. The CryptoSecureRequirement is .
public CreateSecret ( int bits ) : string
bits int The number of bits of entropy to use.
return string

CreateSecret() public method

Creates a secret key with the specified number of bits of entropy and specified CryptoSecureRequirement to be shared with the user on wich the future valid TOTP codes will be based.
/// Thrown when the of the instance is not cryptographically secure and the /// requires a cryptographically secure RNG. ///
public CreateSecret ( int bits, CryptoSecureRequirement cryptoSecureRequirement ) : string
bits int The number of bits of entropy to use.
cryptoSecureRequirement CryptoSecureRequirement The to ensure cryptographically secure RNG's.
return string

GetCode() public method

Gets a TOTP code based on the specified secret for the current time.
public GetCode ( string secret ) : string
secret string The shared secret.
return string

GetCode() public method

Gets a TOTP code based on the specified secret for the specified DateTime.
public GetCode ( string secret, System.DateTime dateTime ) : string
secret string The shared secret.
dateTime System.DateTime The for the TOTP code.
return string

GetCode() public method

Gets a TOTP code based on the specified secret for the specified timestamp.
public GetCode ( string secret, long timestamp ) : string
secret string The shared secret.
timestamp long The timestamp for the TOTP code.
return string

GetQrCodeImageAsDataUri() public method

Retrieves / generates a QR code to be displayed to the user for sharing the shared secret and easy input of this code by scanning with a default size (DEFAULTQRCODESIZE).
public GetQrCodeImageAsDataUri ( string label, string secret ) : string
label string The label to identify which account a key is associated with.
secret string The shared secret.
return string

GetQrCodeImageAsDataUri() public method

Retrieves / generates a QR code to be displayed to the user for sharing the shared secret and easy input of this code by scanning with a specified size.
Thrown when is less than 0
public GetQrCodeImageAsDataUri ( string label, string secret, int size ) : string
label string The label to identify which account a key is associated with.
secret string The shared secret.
size int The desired size, in pixels (width and height equal), of the QR code.
return string

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
public TwoFactorAuth ( ) : System
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
public TwoFactorAuth ( string issuer ) : System
issuer string The issuer of the TOTP authentication token.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when the specified is invalid. ///
public TwoFactorAuth ( string issuer, Algorithm algorithm ) : System
issuer string The issuer of the TOTP authentication token.
algorithm Algorithm The algorithm to use when generating TOTP codes.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when the specified is invalid. /// /// Thrown when the specified is null. ///
public TwoFactorAuth ( string issuer, Algorithm algorithm, IQrCodeProvider qrcodeprovider ) : System
issuer string The issuer of the TOTP authentication token.
algorithm Algorithm The algorithm to use when generating TOTP codes.
qrcodeprovider IQrCodeProvider The to use for generating QR codes.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when the specified is invalid. /// /// Thrown when the specified or is null. ///
public TwoFactorAuth ( string issuer, Algorithm algorithm, IQrCodeProvider qrcodeprovider, IRngProvider rngprovider ) : System
issuer string The issuer of the TOTP authentication token.
algorithm Algorithm The algorithm to use when generating TOTP codes.
qrcodeprovider IQrCodeProvider The to use for generating QR codes.
rngprovider IRngProvider The to use for generating sequences of random numbers.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when the specified is invalid. /// /// Thrown when the specified is null. ///
public TwoFactorAuth ( string issuer, Algorithm algorithm, IRngProvider rngprovider ) : System
issuer string The issuer of the TOTP authentication token.
algorithm Algorithm The algorithm to use when generating TOTP codes.
rngprovider IRngProvider The to use for generating sequences of random numbers.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when the specified is null. ///
public TwoFactorAuth ( string issuer, IQrCodeProvider qrcodeprovider ) : System
issuer string The issuer of the TOTP authentication token.
qrcodeprovider IQrCodeProvider The to use for generating QR codes.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when the specified is null. ///
public TwoFactorAuth ( string issuer, IQrCodeProvider qrcodeprovider, IRngProvider rngprovider ) : System
issuer string The issuer of the TOTP authentication token.
qrcodeprovider IQrCodeProvider The to use for generating QR codes.
rngprovider IRngProvider The to use for generating sequences of random numbers.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when the specified is null. ///
public TwoFactorAuth ( string issuer, IRngProvider rngprovider ) : System
issuer string The issuer of the TOTP authentication token.
rngprovider IRngProvider The to use for generating sequences of random numbers.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when is less than 0. ///
public TwoFactorAuth ( string issuer, int digits ) : System
issuer string The issuer of the TOTP authentication token.
digits int The number of digits to be displayed to the user / required for verification.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when or are less than 0. ///
public TwoFactorAuth ( string issuer, int digits, int period ) : System
issuer string The issuer of the TOTP authentication token.
digits int The number of digits to be displayed to the user / required for verification.
period int The period, specified in seconds, a TOTP is valid.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when or are less than 0 or the specified /// is invalid. ///
public TwoFactorAuth ( string issuer, int digits, int period, Algorithm algorithm ) : System
issuer string The issuer of the TOTP authentication token.
digits int The number of digits to be displayed to the user / required for verification.
period int The period, specified in seconds, a TOTP is valid.
algorithm Algorithm The algorithm to use when generating TOTP codes.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when or are less than 0 or the specified /// is invalid. /// /// Thrown when the specified is null. ///
public TwoFactorAuth ( string issuer, int digits, int period, Algorithm algorithm, IQrCodeProvider qrcodeprovider ) : System
issuer string The issuer of the TOTP authentication token.
digits int The number of digits to be displayed to the user / required for verification.
period int The period, specified in seconds, a TOTP is valid.
algorithm Algorithm The algorithm to use when generating TOTP codes.
qrcodeprovider IQrCodeProvider The to use for generating QR codes.
return System

TwoFactorAuth() public method

Intializes a new instance of the TwoFactorAuth class.
/// Thrown when or are less than 0 or the specified /// is invalid. /// /// Thrown when the specified or is null. ///
public TwoFactorAuth ( string issuer, int digits, int period, Algorithm algorithm, IQrCodeProvider qrcodeprovider, IRngProvider rngprovider ) : System
issuer string The issuer of the TOTP authentication token.
digits int The number of digits to be displayed to the user / required for verification.
period int The period, specified in seconds, a TOTP is valid.
algorithm Algorithm The algorithm to use when generating TOTP codes.
qrcodeprovider IQrCodeProvider The to use for generating QR codes.
rngprovider IRngProvider The to use for generating sequences of random numbers.
return System

VerifyCode() public method

Verifies a TOTP code with the shared secret for the current time and with a DEFAULTDISCREPANCY.
public VerifyCode ( string secret, string code ) : bool
secret string The shared secret.
code string The TOTP code to verify.
return bool

VerifyCode() public method

Verifies a TOTP code with the shared secret for the current time and with a specified discrepancy.
public VerifyCode ( string secret, string code, int discrepancy ) : bool
secret string The shared secret.
code string The TOTP code to verify.
discrepancy int The allowed time discrepancy (in both directions) in number of s.
return bool

VerifyCode() public method

Verifies a TOTP code with the shared secret for the specified DateTime and with a specified discrepancy.
public VerifyCode ( string secret, string code, int discrepancy, System.DateTime dateTime ) : bool
secret string The shared secret.
code string The TOTP code to verify.
discrepancy int The allowed time discrepancy (in both directions) in number of s.
dateTime System.DateTime The for wich to verify the TOTP code.
return bool

VerifyCode() public method

Verifies a TOTP code with the shared secret for the specified timestamp and with a specified discrepancy.
/// Thrown when or is null. ///
public VerifyCode ( string secret, string code, int discrepancy, long timestamp ) : bool
secret string The shared secret.
code string The TOTP code to verify.
discrepancy int The allowed time discrepancy (in both directions) in number of s.
timestamp long The timestamp for wich to verify the TOTP code.
return bool