This library offers support for both Time-Based One-Time Password (TOTP
) and HMAC-based one-time password (HOTP
) protocols, enabling secure and reliable authentication for various applications. With seamless integration for Google
, Microsoft
, and other RFC 6238-compliant
authentication systems
- TOTP (MD5, SHA1, SHA256, SHA384, SHA512)
- HOTP (MD5, SHA1, SHA256, SHA384, SHA512)
- Includes Base32Helper class
You can install the library via NuGet Package Manager:
dotnet add package Ogu.Otp
Totp:
private static readonly TotpTokenProvider _totp = new TotpTokenProvider("MyAppName");
var secretKey = _totp.GenerateSecretKey(); // Save this for further evaluations
string uri = _totp.GetUri("Ogulcan", secretKey); // Needed for generating qr code
// (optional) manually you can generate code ( e.g time based email verification ) but this generally returns from Auth app (Google, Microsoft)
var code = _totp.GenerateCode(secretKey);
var otpValidationResult = _totp.ValidateCode(code, secretKey); // validates the code and returns the validation result
Hotp:
private static readonly HotpTokenProvider _hotp = new HotpTokenProvider("MyAppName");
var secretKey = _hotp.GenerateSecretKey(); // Save this for further evaluations
string uri = _hotp.GetUri("Ogulcan", secretKey); // Needed for generating qr code
// (optional) manually you can generate code ( e.g time based email verification ) but this generally returns from Auth app (Google, Microsoft)
var code = _hotp.GenerateCode(secretKey);
long counter = 0;
var otpValidationResult = _hotp.ValidateCode(code, secretKey, counter);
if (otpValidationResult.IsValid)
{
counter++; // increase the counter and save it to database
}
A sample application demonstrating the usage of Ogu.Otp Console, Otp.Api.
This library makes use of certain codes and resources from AspNetCore, Base32.cs, Rfc6238AuthenticationService.cs