SecureOTP-Generator is a C# library for generating Time-based One-Time Passwords (TOTP) and HMAC-based One-Time Passwords (HOTP), in accordance with the standards specified in RFC 4226 and RFC 6238. These OTPs are commonly used in two-factor authentication (2FA) systems to enhance security.
- TOTP Generation: Generate time-based OTPs with configurable time periods, algorithms, and digits.
- HOTP Generation: Generate counter-based OTPs with configurable counters, algorithms, and digits.
- Configurable Algorithms: Supports MD5, SHA1, SHA256, and SHA512 hashing algorithms.
- Base32 Secret Keys: Utilizes Base32-encoded secret keys for secure OTP generation.
To integrate this library into your project:
Download the lib from the latest releases: https://github.com/StillAching/SecureOTP-Generator/releases Then reference the file in your project.
OR
-
Clone the repository:
git clone https://github.com/StillAching/SecureOTP-Generator.git
-
Add the
.cs
files from the cloned repository to your C# project.
To generate a TOTP, instantiate the TOTP
class and configure it as follows:
using System;
public class Program
{
public static void Main()
{
var totp = new SecureOTPGenerator.TOTP
{
Secret = "PUT SECRET HERE!!!", // Required
Digits = 6, // Optional (default is 6)
Algorithm = SecureOTPGenerator.HashAlgorithm.SHA1, // Optional (default is SHA1)
Period = 30, // Optional (default is 30 seconds) [30 seconds period is common for TOTP]
//UnixTime = 99999999999 // Optional (default is current Unix time)
};
string otp = totp.Generate();
Console.WriteLine($"Generated TOTP: {otp}");
}
}
To generate an HOTP, use the HOTP
class:
using System;
public class Program
{
public static void Main()
{
var hotp = new SecureOTPGenerator.HOTP
{
Secret = "PUT SECRET HERE!!!", // Required
Digits = 6, // Optional (default is 6)
Counter = 0 // Optional (default is 0)
};
string otp = hotp.Generate();
Console.WriteLine($"Generated HOTP: {otp}");
}
}
- Secret: Base32-encoded string used as the secret key. (Required)
- Digits: Number of digits in the OTP (default is 6).
- Algorithm: Hashing algorithm to use (
MD5
,SHA1
,SHA256
,SHA512
). Default is set toSHA1
. - Period: Time period in seconds for the TOTP (default is 30 seconds).
- UnixTime: Unix timestamp for generating the OTP. Defaults to the current time.
- Secret: Base32-encoded string used as the secret key. (Required)
- Digits: Number of digits in the OTP (default is 6).
- Counter: Counter value used for generating the OTP (default is 0).
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.