Hash

From SA-MP Wiki

Jump to: navigation, search

hash

Image:Farm-Fresh text lowercase.png Note: This function name starts with a lowercase letter.


Description:

This function allows you to hash strings, this function can be used to store private information (i.e: passwords)


Image:32px-Circle-style-warning.png

Important
Note

This is not a native SA-MP function, and there are many more advanced methods of encryption in comparison to this old function.


Parameters:
(const string[], key[]="", bit=32)
const string[]The string you want to hash
key[]=""The key you want to use for hash (optional)
bit=32The number of bits the hash should be (optional)


Return Values:

The hashed string


hash("Hash this string"); // returns "NbJU9mSAXSSxfSlbic30TrxmIU4jtyWY"
hash("Hash this String"); // returns "U0zRNIbJgbbjjoOvGt2hu06wRdDbTHYJ"
 
hash("Hash this string","Hashkey"); // returns "HrKfk8bZgWMXjVU77XMz4m6rgtqP7KxK"
hash("Hash this string","HashKey"); // returns "6DNE1DQJDK1a0gFJfEtqmilBTXWgiSca"

Note: As you can see even very similar strings with the same key have two completely different hashes.
You can also see that two equal strings with almost the same key have two completely different hashes.
That's an evidence of it's safety.

How to use:
1) Save your hashed password
2) On logging in, hash the password and compare it with the saved one

(Advice: You can hash userdatas (username and password) to only one hash by using the password as hash-key)

new hashsaved[]="lL5YPmWSalwr7R9k4QgEBHVpICRCGLzT"; // hash("TestUsername","TestPassword")
 
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/login ", cmdtext, true, 7) == 0)
    {
        new username[MAX_STRING], password[MAX_STRING];
        format(username,sizeof(username),left(cmdtext[7],strfind(cmdtext[7]," ")));
        format(password,sizeof(password),right(cmdtext[7],strfind(cmdtext[7]," ")));
        if (strcmp(hash(username,password), hashsaved, false) == 0) // if you wrote "/login TestUsername TestPassword" (lettercase will NOT be ignored)
        {
            SendClientMessage(playerid, 0xFFFFFFFF, "You're succesfully logged in");
        }
        else
        {
            SendClientMessage(playerid, 0xFFFFFFFF, "wrong username or password");
        }
        return 1;
    }
    return 0;
}

Related Functions

The following functions may be useful, as they are related to this function in one way or another.

Personal tools
Navigation
Toolbox