Example of using token generation OATH, validation and blob update to avoid a noreply attack.
using System;
using System.IO;
using QRCoder;
using System.Text;
namespace ConsoleATesteCheckOTPpp1
{
class Program
{
private static string HSM_HOST = "200.201.208.61";
private static string HSM_USER = "tstotp";
private static string HSM_PASS = "12345678";
private static string HSM_KEY = "key_c";
private static string BLOB_PATH = @".\blob_otp.bin";
private static string QRCODE_PATH = @".\seed_otp.jpg";
static void Main(string[] args)
{
client.
Connect(HSM_HOST, HSM_USER, HSM_PASS);
{
}
byte[] blob = null;
if (!File.Exists(BLOB_PATH))
{
blob = client.
OATHIssueGenerateTOTP(HSM_KEY, 30);
File.WriteAllBytes(BLOB_PATH, blob);
}
else
{
blob = File.ReadAllBytes(BLOB_PATH);
}
if (!File.Exists(QRCODE_PATH))
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
byte[] seed = client.
OATHGetKey(HSM_KEY, blob);
string payload = string.Format("otpauth://totp/Dinamo:teste?secret={0}&algorithm=SHA1&digits=6&period=30", seed_base32);
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
System.Drawing.Bitmap fig = qrCode.GetGraphic(20);
fig.Save(QRCODE_PATH, System.Drawing.Imaging.ImageFormat.Jpeg);
}
string otp = "";
client2
.Connect(HSM_HOST, HSM_USER, HSM_PASS);
of
{
Console.Out.Write("Enter the OTP value:");
otp = Console.In.ReadLine();
if (client.
OATHCheck(HSM_KEY, otp, blob, 10))
{
Console.Out.WriteLine("OTP Valid!!!");
File.WriteAllBytes(BLOB_PATH, blob);
}
else
{
Console.Out.WriteLine("OTP Invalid!!!");
}
} while (otp != "");
Console.Out.WriteLine("Generated file");
}
}
}
API class for accessing HSM functionalities Dinamo. In this class you can program using...
Definition DinamoClient.cs:93
KEY_ALG
Definition DinamoClient.cs:227
bool IsKeyExist(string KeyId)
Definition DinamoClient.cs:2066
IntPtr GenerateKey(string KeyId, DinamoClient.KEY_ALG Alg, bool Exportable)
Generates a permanent key in the HSM.
Definition DinamoClient.cs:1891
byte[] OATHIssueGenerateTOTP(string szMasterKeyId)
Generates a TOTP blob, i.e. an event token. The seed will be generated randomly by the HSM.
Definition DinamoClient.cs:5984
bool OATHCheck(string masterKeyId, string otp, byte[] bBlob)
Check OTP value.
Definition DinamoClient.cs:5783
string EncodeBase32(byte[] data)
Utility function for encoding Base32. Standard encoding for OATH generators in sofware.
Definition DinamoClient.cs:6158
byte[] OATHGetKey(string szMasterKey, byte[] pbInBlob)
Retrieves the seed of the key generating the blob from OATH.
Definition DinamoClient.cs:5853
void Connect(string User, string Password)
Establishes an encrypted connection to the HSM using the load balance settings.
Definition DinamoClient.cs:562
void Disconnect(bool flagClose)
Terminates the connection to the HSM.
Definition DinamoClient.cs:814
Namespace denoting a set of functions for accessing the HSM Dinamo and their respective exceptions.
Definition DinamoClient.cs:12