Generare OTP in VB.NET

Mattepuffo's logo
Generare OTP in VB.NET

Generare OTP in VB.NET

OTP sta per One Time Password, ed è sempre più usato per migliorare la sicurezza online; sia per il login che per effettuare altre operazioni.

In genere questi OTP vengono iviati per SMS o per email.

Oggi vediamo come crearli in VB.NET usando la libreria Otp.NET!

Per installarla potete usare NuGet.

Qui sotto il codice completo con un esempio sia per il time based che per il counter based:

Imports OtpNet

Module Program

    Sub Main(args As String())
        Dim secret = New Byte() {&H20, &H20, &H20, &H20, &H20, &H20, &H20}

        creaHotp(secret)
        creaTotp(secret)
    End Sub

    Sub creaHotp(ByVal secret As Byte())
        Dim hotp = New Hotp(secret, mode:=OtpHashMode.Sha512)
        Dim hotpCode = hotp.ComputeHOTP(4)
        Dim verified = hotp.VerifyHotp(hotpCode, 4)
        Console.WriteLine(verified)
    End Sub

    Sub creaTotp(ByVal secret As Byte())
        Dim timeWindowUsed As Long
        Dim totp = New Totp(secret, mode:=OtpHashMode.Sha512)
        Dim totpCode = totp.ComputeTotp(DateTime.UtcNow)
        Dim remainingTime = totp.RemainingSeconds()
        Dim remainingSeconds = totp.RemainingSeconds(DateTime.UtcNow)
        Dim verified = totp.VerifyTotp(totpCode, timeWindowUsed, VerificationWindow.RfcSpecifiedNetworkDelay)
        Console.WriteLine(totpCode)
        Console.WriteLine(remainingTime)
        Console.WriteLine(remainingSeconds)
        Console.WriteLine(timeWindowUsed)
        Console.WriteLine(verified)
    End Sub

End Module

La documentazione non è proprio un granchè.

Per prenderci un po la mano vi conviene giocherellare un pò con il codice.

Per il counter based, considerate sempre che il conteggio glielo passiamo statico nel codice; nella realtà il conteggio andrà salvato da qualche parte.

Enjoy!


Condividi

Commentami!