Imports System.Security.Cryptography
Imports Microsoft.VisualBasic
Imports Konscious.Security.Cryptography
Public Class VERAG_VARIABLES
    Public Shared errornumb As Integer = 0
    Shared Function getiterationnumber() As Integer
        Return RandomInteger(Math.Pow(2, 4), Math.Pow(2, 11))
    End Function

    Shared Sub initerrorcount()
        errornumb = 0
    End Sub
    Shared Sub seterrorcount(var As Integer)
        errornumb = var
    End Sub
    Shared Function geterrornumb() As String
        Return Environment.NewLine + "Error:" + Space(1) + VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(errornumb.ToString) + Space(1)
    End Function
    Shared Function getErrorcodeindez(Errorcode As String) As String
        Return VERAG_PROG_ALLGEMEIN.cCryptography3.Decrypt(Errorcode)
    End Function
    Public Shared Function GenerateSalt(ByVal nSalt As Integer) As Byte()
        Dim saltBytes = New Byte(nSalt) {}
        Using provider = New RNGCryptoServiceProvider()
            provider.GetNonZeroBytes(saltBytes)
        End Using
        Return saltBytes
        'Convert.ToBase64String(saltBytes)
    End Function
    Public Shared Async Function HashPassword(password As String, salt As Byte(), nIterations As Integer, nHash As Integer) As Threading.Tasks.Task(Of Byte())
        'Dim Argon As Argon2id = New Argon2id(Encoding.UTF8.GetBytes(password))
        If HttpContext.Current.Request.ServerVariables("SERVER_NAME") = "localhost" Then
            Dim Argon As Argon2d = New Argon2d(Encoding.UTF8.GetBytes(VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(password)))
            Argon.Salt = salt
            Argon.DegreeOfParallelism = 24
            Argon.Iterations = nIterations
            Argon.MemorySize = (((nIterations * 1 - nIterations / 4 * 2) / 1.05) + 1 * 150)
            Return Await Argon.GetBytesAsync(nHash)
            'Return Convert.ToBase64String(Argon.GetBytes(nHash))
            'Return Argon.GetBytes(nHash)
            'Dim saltBytes = Convert.FromBase64String(salt)
            'Using rfc2898DeriveBytes = New Rfc2898DeriveBytes(password, saltBytes, nIterations)
            'End Using
        Else
            Dim Argon As Argon2d = New Argon2d(Encoding.UTF8.GetBytes(VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(password)))
            Argon.Salt = salt
            Argon.DegreeOfParallelism = 48
            Argon.Iterations = nIterations
            Argon.MemorySize = ((((nIterations * 2 - nIterations / 1.75 * 2.08) / 0.08) + 1 * 256) / 2)
            Return Await Argon.GetBytesAsync(nHash)
        End If
    End Function

    Public Shared Async Function Verifyhash(ByVal passw As String, ByVal salt As Byte(), ByVal hash As Byte(), ByVal nIterations As Integer, ByVal nHash As Integer) As Threading.Tasks.Task(Of Boolean)
        Dim newHash As Byte() = Await HashPassword(passw, salt, nIterations, nHash)
        Return hash.SequenceEqual(newHash)
    End Function
    Public Shared Function RandomInteger(ByVal min As Integer, ByVal _
    max As Integer) As Integer
        Dim rand As New RNGCryptoServiceProvider()
        Dim one_byte() As Byte = {0}
        rand.GetBytes(one_byte)
        Return min + (max - min) * (one_byte(0) / 255)
    End Function
End Class
