WP_Hash/mySQL Password Problem
#1

Hello, i've been working on the register and login system of a new project!

But i have a problem with password hashing, I am using Whirlpool hashing function, but the problem is that when the player registers there is nothing in the "Password" field it's blank, so people can login with random passwords on the account

Here's a picture:

and here is my code

PHP код:
// Includes
#include <a_samp>
#include <a_mysql>
// Native WP_Hash On top of the script
native WP_Hash(buffer[], len, const str[]);  
// mysql defines
#define mysql_host "localhost"
#define mysql_user "root"
#define mysql_password ""
#define mysql_database "sfrp"
// Dialogs defines
#define RegDialog 0
#define LoginDialog 1
// playerdata
enum playerInfo
{
    
Password[129],
    
skin,
    
db_id
}
new 
pInfo[MAX_PLAYERS][playerInfo];
// forwards
forward CheckUser(playerid);
forward CheckPassword(playerid);
// mysql variable
new dbhandle;
// OnGameModeInit
    
dbhandle mysql_connect(mysql_hostmysql_usermysql_databasemysql_password);
    if(
mysql_errno() != 0
    { 
        
printf("[MySQL] Failed to connect."); 
    } 
    else 
    { 
        
printf("[MySQL] Succesfully connected."); 
    } 
// OnGameModeExit
    
mysql_close(dbhandle);
// OnPlayerConnect
    
new query[128];
    
mysql_format(dbhandlequerysizeof(query), "SELECT `Password`, `ID` FROM `users` WHERE `Username` = '%e' LIMIT 1"PlayerName(playerid)); 
    
mysql_tquery(dbhandlequery"CheckUser""i"playerid); 
// OnPlayerDisconnect
// SavePlayerStats
    
SavePlayerStats(playerid);
    
ResetPlayerStats(playerid);
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    
/***************
      Login Dialog
    ****************/
    
if(dialogid == LoginDialog)
    {
        if(
response)
        {    
            if (
strlen(inputtext) > 0)
               {    
                   new 
hashpass[129], query[128];
                   
WP_Hash(hashpasssizeof(hashpass), inputtext); 
                if(!
strcmp(hashpass,  pInfo[playerid][Password]))
                {
                    
mysql_format(dbhandlequerysizeof(query), "SELECT * FROM `users` WHERE `Username` = '%e' LIMIT 1"PlayerName(playerid)); 
                    
mysql_tquery(dbhandlequery"CheckPassword""i"playerid); 
                }
            }
            else
            {
                new 
string[159 MAX_PLAYER_NAME];
                
format(stringsizeof(string),"Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {00FF00}Registered.\n\n{FF0000}You must type the password related to this account to login."PlayerName(playerid));
                
ShowPlayerDialog(playeridLoginDialogDIALOG_STYLE_PASSWORD"{00FF00}Account found - Logging in..."string"Login""Cancel");
             }
        }
        else
        {
            
Kick(playerid);
        }
        return 
1;
    }
    
/***************
    Register Dialog
    ****************/
    
if(dialogid == RegDialog)
    {
        if(
response)
        {
            if (
strlen(inputtext) > 0)
            {
                new 
query[128];
                
WP_Hash(pInfo[playerid][Password], 129inputtext);
                
mysql_format(dbhandlequerysizeof(query), "INSERT INTO `users` (`Username`, `Password`) VALUES ('%e', '%e')"PlayerName(playerid), pInfo[playerid][Password]); 
                
mysql_tquery(dbhandlequery"""");
            }
            else
            {
                new 
string[145 MAX_PLAYER_NAME];
                
format(stringsizeof(string), "Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {FF0000}Unregistered.\n\nYou must create a password below to register this account."PlayerName(playerid));
                
ShowPlayerDialog(playeridRegDialogDIALOG_STYLE_INPUT"{FF0000}Account not found - Registering..."string"Register""Cancel");
            }        
        }
        else
        {
            
Kick(playerid);
        }
        return 
1;
    }
    return 
1;
}
// Functions
SavePlayerStats(playerid)
{
    new 
query[128];
    
mysql_format(dbhandlequerysizeof(query), "UPDATE `users` SET `Skin` = %i WHERE `ID` = %i"GetPlayerSkin(playerid), pInfo[playerid][db_id]);
    
mysql_tquery(dbhandlequery""""); 
    return 
1;
}
ResetPlayerStats(playerid)
{
    for (new 
i=0isizeof(pInfo[]); i++)
    {
        
pInfo[playerid][playerInfo:i] = 0;
    }
    return 
1;
}
// Callbacks
public CheckUser(playerid)
{
    new 
rowsfields;
    
cache_get_data(rowsfieldsdbhandle);
    if (
rows == 0)
    {
        new 
string[152 MAX_PLAYER_NAME];
        
format(stringsizeof(string), "Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {FF0000}Unregistered.\n\n{9AB3D0}Create a password below to register this account."PlayerName(playerid));
        
ShowPlayerDialog(playeridRegDialogDIALOG_STYLE_INPUT"{FF0000}Account not found - Registering..."string"Register""Cancel");
    }
    else
    {
        new 
string[152 MAX_PLAYER_NAME];
        
cache_get_field_content(0"Password"pInfo[playerid][Password], dbhandle129);
        
pInfo[playerid][db_id] = cache_get_field_content_int(0"ID");
        
format(stringsizeof(string), "Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {00FF00}Registered.\n\n{9AB3D0}Type the password related to this account to login."PlayerName(playerid));
        
ShowPlayerDialog(playeridLoginDialogDIALOG_STYLE_PASSWORD"{00FF00}Account found - Logging in..."string"Login""Cancel");
    }
    return 
1;
}
public 
CheckPassword(playerid)
{
    
pInfo[playerid][skin] = cache_get_field_content_int(0"Skin");
    return 
1;

Hope you can help me out with this one, i've been trying to fix this for some hours already and still can't figure out the problem!

Thank You for reading!
Reply
#2

Make sure the field is set to CHAR(128). Enable debug and check the logs. Also the length for your insert query is way too short, considerint that the password itself is already 128 chars long. Moreover, you should add a salt. I would recommend you use SHA256_PassHash() so you don't need to load an entire plugin, but salts can be applied to any hash so it's ultimately your choice.
Reply
#3

Looks like it was because of the query lenght, and i'll see that SHA256 too!

Thank you very much for the help
Reply
#4

Sorry for double posting but i'm having 1 more litle problem, strcmp is not working correctly, i check if the password matches with the inputtext, but it is not working, i can login with every password!

PHP код:
// Includes 
#include <a_samp> 
#include <a_mysql> 
// Native WP_Hash On top of the script 
native WP_Hash(buffer[], len, const str[]);   
// mysql defines 
#define mysql_host "localhost" 
#define mysql_user "root" 
#define mysql_password "" 
#define mysql_database "sfrp" 
// Dialogs defines 
#define RegDialog 0 
#define LoginDialog 1 
// playerdata 
enum playerInfo 

    
Password[129], 
    
skin
    
db_id 

new 
pInfo[MAX_PLAYERS][playerInfo]; 
// forwards 
forward CheckUser(playerid); 
forward CheckPassword(playerid); 
// mysql variable 
new dbhandle
// OnGameModeInit 
    
dbhandle mysql_connect(mysql_hostmysql_usermysql_databasemysql_password); 
    if(
mysql_errno() != 0)  
    {  
        
printf("[MySQL] Failed to connect.");  
    }  
    else  
    {  
        
printf("[MySQL] Succesfully connected.");  
    }  
// OnGameModeExit 
    
mysql_close(dbhandle); 
// OnPlayerConnect 
    
new query[128]; 
    
mysql_format(dbhandlequerysizeof(query), "SELECT `Password`, `ID` FROM `users` WHERE `Username` = '%e' LIMIT 1"PlayerName(playerid));  
    
mysql_tquery(dbhandlequery"CheckUser""i"playerid);  
// OnPlayerDisconnect 
// SavePlayerStats 
    
SavePlayerStats(playerid); 
    
ResetPlayerStats(playerid); 
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[]) 

    
/*************** 
      Login Dialog 
    ****************/ 
    
if(dialogid == LoginDialog
    { 
        if(
response
        {     
            if (
strlen(inputtext) > 0
               {     
                   new 
hashpass[129], query[128]; 
                   
WP_Hash(hashpasssizeof(hashpass), inputtext);  
                if(!
strcmp(hashpass,  pInfo[playerid][Password])) 
                { 
                    
mysql_format(dbhandlequerysizeof(query), "SELECT * FROM `users` WHERE `Username` = '%e' LIMIT 1"PlayerName(playerid));  
                    
mysql_tquery(dbhandlequery"CheckPassword""i"playerid);  
                } 
            } 
            else 
            { 
                new 
string[159 MAX_PLAYER_NAME]; 
                
format(stringsizeof(string),"Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {00FF00}Registered.\n\n{FF0000}You must type the password related to this account to login."PlayerName(playerid)); 
                
ShowPlayerDialog(playeridLoginDialogDIALOG_STYLE_PASSWORD"{00FF00}Account found - Logging in..."string"Login""Cancel"); 
             } 
        } 
        else 
        { 
            
Kick(playerid); 
        } 
        return 
1
    } 
    
/*************** 
    Register Dialog 
    ****************/ 
    
if(dialogid == RegDialog
    { 
        if(
response
        { 
            if (
strlen(inputtext) > 0
            { 
                new 
query[200]; 
                
WP_Hash(pInfo[playerid][Password], 129inputtext); 
                
mysql_format(dbhandlequerysizeof(query), "INSERT INTO `users` (`Username`, `Password`) VALUES ('%e', '%e')"PlayerName(playerid), pInfo[playerid][Password]);  
                
mysql_tquery(dbhandlequery""""); 
            } 
            else 
            { 
                new 
string[145 MAX_PLAYER_NAME]; 
                
format(stringsizeof(string), "Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {FF0000}Unregistered.\n\nYou must create a password below to register this account."PlayerName(playerid)); 
                
ShowPlayerDialog(playeridRegDialogDIALOG_STYLE_INPUT"{FF0000}Account not found - Registering..."string"Register""Cancel"); 
            }         
        } 
        else 
        { 
            
Kick(playerid); 
        } 
        return 
1
    } 
    return 
1

// Functions 
SavePlayerStats(playerid

    new 
query[128]; 
    
mysql_format(dbhandlequerysizeof(query), "UPDATE `users` SET `Skin` = %i WHERE `ID` = %i"GetPlayerSkin(playerid), pInfo[playerid][db_id]); 
    
mysql_tquery(dbhandlequery"""");  
    return 
1

ResetPlayerStats(playerid

    for (new 
i=0isizeof(pInfo[]); i++) 
    { 
        
pInfo[playerid][playerInfo:i] = 0
    } 
    return 
1

// Callbacks 
public CheckUser(playerid

    new 
rowsfields
    
cache_get_data(rowsfieldsdbhandle); 
    if (
rows == 0
    { 
        new 
string[152 MAX_PLAYER_NAME]; 
        
format(stringsizeof(string), "Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {FF0000}Unregistered.\n\n{9AB3D0}Create a password below to register this account."PlayerName(playerid)); 
        
ShowPlayerDialog(playeridRegDialogDIALOG_STYLE_INPUT"{FF0000}Account not found - Registering..."string"Register""Cancel"); 
    } 
    else 
    { 
        new 
string[152 MAX_PLAYER_NAME]; 
        
cache_get_field_content(0"Password"pInfo[playerid][Password], dbhandle129); 
        
pInfo[playerid][db_id] = cache_get_field_content_int(0"ID"); 
        
format(stringsizeof(string), "Hello %s, Welcome to San Fierro Roleplay.\n\nAccount status: {00FF00}Registered.\n\n{9AB3D0}Type the password related to this account to login."PlayerName(playerid)); 
        
ShowPlayerDialog(playeridLoginDialogDIALOG_STYLE_PASSWORD"{00FF00}Account found - Logging in..."string"Login""Cancel"); 
    } 
    return 
1

public 
CheckPassword(playerid

    
pInfo[playerid][skin] = cache_get_field_content_int(0"Skin"); 
    return 
1

This is quite bothering me because i can't see from where the bugs comes from, as everything seems to fine
Reply
#5

Little bump
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)