> For the complete documentation index, see [llms.txt](https://beytullahakyuz.gitbook.io/otplib/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://beytullahakyuz.gitbook.io/otplib/.net/usage.md).

# Usage

```csharp
using OTPLib;
```

## TOTP&#x20;

{% tabs %}
{% tab title="Usage-SHA1" %}

```csharp
// Default: SHA1

string secretkey = "ADII2YLBLAAE5DLV36M7ZXXFIJT64753ZR3CSSWWVTRJE7WEWSDG3YVCKGYHSWL45SIJYRLX25UILRBFRTW35GUHJUBHCQXLWXEHG2I=";
TOTP totp = new TOTP(30, 6);
//TOTP totp = new TOTP(30, 6, OTPHashAlgorithm.SHA1);
string otpcode = totp.GenerateOTP(secretkey);
int timeleft = totp.GetRemainingTime();

Console.WriteLine("OTP code: {0}\r\nTimeleft: {1}", otpcode, timeleft.ToString());
```

{% endtab %}

{% tab title="Usage-SHA256" %}

```csharp
// SHA256

string secretkey = "JBSWY3DPEHPK3PXP";
TOTP totp = new TOTP(30, 6, OTPHashAlgorithm.SHA256);
string otpcode = totp.GenerateOTP(secretkey);
int timeleft = totp.GetRemainingTime();

Console.WriteLine("OTP code: {0}\r\nTimeleft: {1}", otpcode, timeleft.ToString());

// Set Hash Algorithm - SHA512
_totp.SetAlgorithm(OTPHashAlgorithm.SHA512);
```

{% endtab %}

{% tab title="Verification" %}

```csharp
string secretkey = "JBSWY3DPEHPK3PXP";
TOTP totp = new TOTP(period, digits, algorithm);
bool verificationstatus = totp.Verify("otpcode", secretkey);
```

{% endtab %}

{% tab title="URL Generation" %}

<pre class="language-csharp"><code class="lang-csharp">// accountname: string, servicename: string, secretkey: Base32-key
string secretkey = "JVDSOVL2IZVGSWB4H5XG6WKGON7G4W2TLNKW27JL";
TOTP totp = new TOTP(period, digits, algorithm);
string otpurl = totp.GetOTPUrl(accountname, servicename, secretkey);
<strong>//string otpurl = totp.GetOTPUrl("user@gmail.com", "Microsoft", "JBSWY3DPEHPK3PXP", period, digits, OTPHashAlgorithm.SHA1);
</strong></code></pre>

{% endtab %}
{% endtabs %}

## HOTP

{% tabs %}
{% tab title="Usage-SHA1" %}

```csharp
// Default: SHA1

string secretkey = "JVDSOVL2IZVGSWB4H5XG6WKGON7G4W2TLNKW27JL";
HOTP hotp = new HOTP(1000, 8, OTPHashAlgorithm.SHA1);
string otpcode = hotp.GenerateOTP(secretkey);
```

{% endtab %}

{% tab title="Usage-SHA256" %}

```csharp
string secretkey = "JVDSOVL2IZVGSWB4H5XG6WKGON7G4W2TLNKW27JL";
HOTP hotp = new HOTP(1000, 8, OTPHashAlgorithm.SHA256);
//hotp.SetAlgorithm(OTPHashAlgorithm.SHA256);
string otpcode = hotp.GenerateOTP(secretkey);
//string otpcode = hotp.GenerateOTP(secretkey, OTPHashAlgorithm.SHA256);
```

{% endtab %}

{% tab title="Verification" %}

```csharp
string secretkey = "JBSWY3DPEHPK3PXP";
HOTP hotp = new HOTP(counter, digits, algorithm);
//hotp.SetCounter(652);
bool verificationstatus = hotp.Verify("otpcode", secretkey);
//bool verificationstatus = hotp.Verify("otpcode", secretkey, OTPHashAlgorithm.SHA256);
```

{% endtab %}

{% tab title="URL Generation" %}

```csharp
// accountname: string, servicename: string, secretkey: Base32-key

string secretkey = "JVDSOVL2IZVGSWB4H5XG6WKGON7G4W2TLNKW27JL";
HOTP hotp = new HOTP(counter, digits, algorithm);
string otpurl = hotp.GetOTPUrl(accountname, servicename, secretkey);
string otpurl = hotp.GetOTPUrl("user@gmail.com", "Microsoft", secretkey, counter, digits, OTPHashAlgorithm.SHA1);
```

{% endtab %}
{% endtabs %}

## Static Functions

<pre class="language-csharp"><code class="lang-csharp">// Encoding Base32 (TOTP or HTOP)

string secretkey = "secret-key";
string encodedSecretkey = TOTP.Base32Encode(UTF8Encoding.UTF8.GetBytes(secretkey));
//string encodedSecretkey = HOTP.Base32Encode(UTF8Encoding.UTF8.GetBytes(secretkey));

// Check Valid Secretkey (TOTP or HTOP)
string secretkey = "OBGFELDJJRGWG4DSNFBCENSBMZNUM43J";
bool checksecretkey = OTPLib.OTPLib.CheckValidSecretkey(secretkey);
//bool checksecretkey = HOTP.CheckValidSecretkey(secretkey);
//bool checksecretkey = TOTP.CheckValidSecretkey(secretkey);

// Generate Random Secret Key
string generatedsecretkey = TOTP.GenerateRandomSecretKey(30);
<strong>//string generatedsecretkey = HOTP.GenerateRandomSecretKey(40);
</strong></code></pre>
