Hello i'm having issue with this code, It creates record in database but the password is not correct when i try to login ingame it says wrong.
Can someone help me out how to make it working?
https://github.com/TrinityCore/old_basic-account-creator/blob/master/Trinity%20Account%20Creator/php/createAccount.php
https://github.com/TrinityCore/old_basic-account-creator/blob/master/Trinity%20Account%20Creator/php/db.php
private function calculateSRP6Verifier($username, $password, $salt)
{
// algorithm constants
$g = gmp_init(7);
$N = gmp_init('894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7', 16);
// calculate first hash
$h1 = sha1(strtoupper($username . ':' . $password), TRUE);
// calculate second hash
$h2 = sha1($salt.$h1, TRUE);
// convert to integer (little-endian)
$h2 = gmp_import($h2, 1, GMP_LSW_FIRST);
// g^h2 mod N
$verifier = gmp_powm($g, $h2, $N);
// convert back to a byte array (little-endian)
$verifier = gmp_export($verifier, 1, GMP_LSW_FIRST);
// pad to 32 bytes, remember that zeros go on the end in little-endian!
$verifier = str_pad($verifier, 32, chr(0), STR_PAD_RIGHT);
// done!
return $verifier;
}
// Returns SRP6 parameters to register this username/password combination with
public function getRegistrationData($username, $password)
{
// generate a random salt
$salt = random_bytes(32);
// calculate verifier using this salt
$verifier = $this->calculateSRP6Verifier($username, $password, $salt);
// done - this is what you put in the account table!
return array($salt, $verifier);
}
// Close the database connection.
public function close() {
$this->conn = null;
}
// Get the SHA1 encrypted password.
list($salt, $verifier) = $db->getRegistrationData($username, $password);
$accountCreateQuery = "INSERT INTO account(username, salt, verifier, email) VALUES(?, ?, ?, ?)";
$accountCreateParams = array($username, $salt, $verifier, $email);
// Execute the query.
$db->insertQuery($accountCreateQuery, $accountCreateParams);
// Close connection to the database.
$db->close();
Here is what i made:
Imports System.Security.Cryptography
Imports System.Numerics
Public Class SRP6
Public Shared Function CalculateSRP6Verifier(ByVal username As String, ByVal password As String, ByVal salt As Byte()) As Byte()
' algorithm constants
Dim g As BigInteger = BigInteger.Parse("7")
Dim N As BigInteger = BigInteger.Parse("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7", Globalization.NumberStyles.HexNumber)
' calculate first hash
Dim h1 As Byte() = SHA1.Create().ComputeHash(Text.Encoding.UTF8.GetBytes(username.ToUpper() & ":" & password.ToUpper()))
' calculate second hash
Dim h2 As Byte() = SHA1.Create().ComputeHash(salt.Concat(h1).ToArray())
' convert to integer (little-endian)
Dim h2Int As New BigInteger(h2.Reverse().ToArray())
' g^h2 mod N
Dim verifier As BigInteger = BigInteger.ModPow(g, h2Int, N)
' convert back to a byte array (little-endian)
Dim verifierBytes As Byte() = verifier.ToByteArray().Reverse().ToArray()
' pad to 32 bytes, remember that zeros go on the end in little-endian!
Array.Resize(verifierBytes, 32)
' done!
Return verifierBytes
End Function
Public Shared Function ReturnResults(ByVal Username As String, ByVal Password As String)
Dim salt(31) As Byte
Using rng As New RNGCryptoServiceProvider()
rng.GetBytes(salt)
End Using
' calculate verifier using this salt
Dim verifier As Byte() = CalculateSRP6Verifier(Username, Password, salt)
' done - this is what you put in the account table!
Dim newSalt As Byte() = salt
Dim newVerifier As Byte() = verifier
'Dim result As Integer = $mysqli_auth->query("INSERT INTO account (username, email, salt, verifier) VALUES ('$username', '$email', '$newSalt', '$newVerifier')")
'Dim query As String = String.Format("INSERT INTO account (username, email, salt, verifier) VALUES ('{0}', '{1}', @salt, @verifier)", Username, email)
'Using command As New MySQLCommand(query, connection)
' command.Parameters.AddWithValue("@salt", newSalt)
' command.Parameters.AddWithValue("@verifier", newVerifier)
' command.ExecuteNonQuery()
'End Using
Return verifier
End Function
'Using other method here
'Same logic different style
Public Shared Function GetSRP6RegistrationData(username As String, password As String) As Byte()
' generate a random salt
Dim salt(31) As Byte
Using rngCsp As New RNGCryptoServiceProvider()
rngCsp.GetBytes(salt)
End Using
' calculate verifier using this salt
Dim verifier As Byte() = CalculateSRP6Verifier(username, password, salt)
' done - this is what you put in the account table!
Return verifier
End Function
Public Shared Function VerifySRP6Login(username As String, password As String, salt As Byte(), verifier As Byte()) As Boolean
' re-calculate the verifier using the provided username + password and the stored salt
Dim checkVerifier As Byte() = CalculateSRP6Verifier(username, password, salt)
' compare it against the stored verifier
Return verifier.SequenceEqual(checkVerifier)
End Function
End Class
Public Sub RegisterUser()
Dim conStr = "Server=" + Data.Settings.MySQLServerHost + ";Uid=" + Data.Settings.MySQLServerUser + ";Database=" + Data.Settings.AuthDatabase + ";Port=" + Data.Settings.MySQLServerPort + ";Pwd=" + Data.Settings.MySQLServerPassword + ";"
'Dim result As Integer = $mysqli_auth->query("INSERT INTO account (username, email, salt, verifier) VALUES ('$username', '$email', '$newSalt', '$newVerifier')")
Try
' Get the SHA1 encrypted password.
Dim salt As Byte() = New Byte(31) {}
Using rng As RNGCryptoServiceProvider = RandomNumberGenerator.Create()
rng.GetBytes(salt)
End Using
' calculate verifier using this salt
Dim verifier As Byte() = SRP6.CalculateSRP6Verifier(TextAccountCreateName.Text, TextAccountPasswordCreate.Text, salt)
Dim newSalt As Byte() = salt
Dim newVerifier As Byte() = verifier
'Dim registrationData = SRP6Enc.GetRegistrationData(TextAccountCreateName.Text, TextAccountPasswordCreate.Text)
Using conn As New MySqlConnection(conStr)
Using cmd As New MySqlCommand()
cmd.Connection = conn
Select Case Data.Settings.SelectedCore
Case Cores.AzerothCore
cmd.CommandText = "INSERT INTO account (username, email, salt, verifier) VALUES (@user,@ema,@pass,@verif)"
cmd.Parameters.AddWithValue("@user", TextAccountCreateName.Text)
cmd.Parameters.AddWithValue("@ema", TextAccountEmailCreate.Text)
cmd.Parameters.AddWithValue("@verif", newVerifier)
If Data.Settings.EnableDBEncrypt And Data.Settings.DatabaseEncryption >= 1 Then
cmd.Parameters.AddWithValue("@pass", newSalt)
Else
cmd.Parameters.AddWithValue("@pass", TextAccountPasswordCreate.Text)
End If
Case Else
Exit Sub
End Select
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub