109 lines
5.3 KiB
VB.net
109 lines
5.3 KiB
VB.net
Imports System.Security.Cryptography
|
|
Imports Microsoft.VisualBasic
|
|
Imports Konscious.Security.Cryptography
|
|
Imports System.Threading.Tasks
|
|
Imports System.Web.Script.Services
|
|
Imports System.Web.Services
|
|
Imports System.Management
|
|
|
|
Public Class VERAG_VARIABLES
|
|
Public Shared errornumb As Integer = 0
|
|
Public Shared Function GetCpuSpeed() As UInteger
|
|
Dim managementObject = New ManagementObject("Win32_Processor.DeviceID='CPU0'")
|
|
Dim speed As UInteger = CUInt(managementObject("CurrentClockSpeed"))
|
|
managementObject.Dispose()
|
|
Return speed
|
|
End Function
|
|
Public Shared Function GetMaxCpuSpeed() As UInteger
|
|
Dim managementObject = New ManagementObject("Win32_Processor.DeviceID='CPU0'")
|
|
Dim speed As UInteger = CUInt(managementObject("MaxClockSpeed"))
|
|
managementObject.Dispose()
|
|
Return speed
|
|
End Function
|
|
Shared Function getiterationnumber() As Integer
|
|
If HttpContext.Current.Request.ServerVariables("SERVER_NAME") = "localhost" Then
|
|
Return RandomInteger(Math.Pow(2, 2), (0.45 * GetCpuSpeed()))
|
|
Else
|
|
Return RandomInteger(Math.Pow(2, 3), (0.47 * GetMaxCpuSpeed()))
|
|
End If
|
|
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 Function HashPassword(password As String, salt As Byte(), nIterations As Integer, nHash As Integer) As Byte()
|
|
'Dim Argon As Argon2id = New Argon2id(Encoding.UTF8.GetBytes(password))
|
|
|
|
If HttpContext.Current.Request.ServerVariables("SERVER_NAME") = "localhost" Then
|
|
Dim Argon As Argon2id = New Argon2id(Encoding.UTF8.GetBytes(VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(password)))
|
|
Argon.Salt = salt
|
|
Argon.DegreeOfParallelism = 28
|
|
Argon.Iterations = nIterations * VERAG_VARIABLES.RandomInteger(2, 3)
|
|
Argon.MemorySize = (((nIterations * 9.98 - (nIterations * 10.23) / 14 * 2) / 4.058) + 1 * 190)
|
|
Return Argon.GetBytes(nHash)
|
|
Else
|
|
Dim Argon As Argon2id = New Argon2id(Encoding.UTF8.GetBytes(VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(password)))
|
|
Argon.Salt = salt
|
|
Argon.DegreeOfParallelism = 18
|
|
Argon.Iterations = nIterations * VERAG_VARIABLES.RandomInteger(2, 4)
|
|
Argon.MemorySize = (((nIterations * 9.88 - (nIterations * 10.23) / 8.85 * 2.28) / 10.28) + 2 * 878) / 13.1
|
|
Return Argon.GetBytes(nHash)
|
|
End If
|
|
|
|
End Function
|
|
Public Shared Async Function HashPasswordAsync(password As String, salt As Byte(), nIterations As Integer, nHash As Integer) As 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 Argon2id = New Argon2id(Encoding.UTF8.GetBytes(VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(password)))
|
|
Argon.Salt = salt
|
|
Argon.DegreeOfParallelism = 24
|
|
Argon.Iterations = nIterations * VERAG_VARIABLES.RandomInteger(2, 3)
|
|
Argon.MemorySize = (((nIterations * 15.98 - (nIterations * 10.23) / 14 * 2) / 4.058) + 1 * 190)
|
|
Return Await Argon.GetBytesAsync(nHash)
|
|
Else
|
|
Dim Argon As Argon2id = New Argon2id(Encoding.UTF8.GetBytes(VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(password)))
|
|
Argon.Salt = salt
|
|
Argon.DegreeOfParallelism = 28
|
|
Argon.Iterations = nIterations * VERAG_VARIABLES.RandomInteger(2, 4)
|
|
Argon.MemorySize = (((nIterations * 10.88 - (nIterations * 10.23) / 2.85 * 2.28) / 10.28) + 2 * 178) / 13.1
|
|
Return Await Argon.GetBytesAsync(nHash)
|
|
End If
|
|
End Function
|
|
|
|
Public Shared Function Verifyhash(ByVal passw As String, salt As Byte(), ByVal hash As Byte(), ByVal nIterations As Integer, ByVal nHash As Integer) As Boolean
|
|
Dim newHash As Byte() = HashPassword(passw, salt, nIterations, nHash)
|
|
Return hash.SequenceEqual(newHash)
|
|
End Function
|
|
Public Shared Async Function VerifyhashAsync(ByVal passw As String, salt As Byte(), ByVal hash As Byte(), ByVal nIterations As Integer, ByVal nHash As Integer) As Task(Of Boolean)
|
|
Dim newHash As Byte() = Await HashPasswordAsync(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
|