Usage
using OTPLib;
TOTP
// 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());
// 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);
string secretkey = "JBSWY3DPEHPK3PXP";
TOTP totp = new TOTP(period, digits, algorithm);
bool verificationstatus = totp.Verify("otpcode", secretkey);
// accountname: string, servicename: string, secretkey: Base32-key
string secretkey = "JVDSOVL2IZVGSWB4H5XG6WKGON7G4W2TLNKW27JL";
TOTP totp = new TOTP(period, digits, algorithm);
string otpurl = totp.GetOTPUrl(accountname, servicename, secretkey);
//string otpurl = totp.GetOTPUrl("user@gmail.com", "Microsoft", "JBSWY3DPEHPK3PXP", period, digits, OTPHashAlgorithm.SHA1);
HOTP
// Default: SHA1
string secretkey = "JVDSOVL2IZVGSWB4H5XG6WKGON7G4W2TLNKW27JL";
HOTP hotp = new HOTP(1000, 8, OTPHashAlgorithm.SHA1);
string otpcode = hotp.GenerateOTP(secretkey);
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);
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);
// 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);
Static Functions
// 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);
//string generatedsecretkey = HOTP.GenerateRandomSecretKey(40);
Last updated