I wrote a script that one way encrypts a user's password by generating a key, and multiplies by the ASCII value of the character and the ASCII value of the key character at (the position that the character has in the password string) and repeats this for the previous positions in the string meaning a key string with ASCII values of:
49, 50, 51, 52, 53, 54, 55, 56, 57, 48
and a password string of
112, 97, 115, 115, 119, 111, 114, 100, 46, 49
Would be multiplied like:
[(112 * 49)] + [(97 * 50) + (97 * 49)] + ...
and so on.
As an example, the string password.1
encrypted with the key 1234567890
(auto generated) would return 203456
.
$pass = $_GET["pass"];
$key = $_GET['key'];
$newPass = go($pass, $key);
echo $newPass;
function go($pass, $key){
$passArray = str_split($pass);
$keyArray = str_split($key);
$total = 0;
$pos = 0;
foreach ($passArray as $char){
$posInitial = $pos;
while($pos !== 0){
$total = $total + (ord($keyArray[$pos]) * ord($char));
$pos--;
}
$pos = $posInitial + 1;
}
return $total;
}
function authorise($pass, $passEncode, $key){
$passEncodeTest = go($pass, $key);
if ($passEncode == $passEncodeTest){
return true;
} else {
return false;
}
}
authorise('password.2', $newPass, $key); ==> returns false
authorise('password.1', $newPass, $key); ==> returns true
(I will add a password sanitiser in later, but for now, plaintext)
My question is, what can I do to improve this code?
password_hash()
function. \$\endgroup\$authorise
is used to grant/deny permissions. Your method should be renamed toauthenticate
. \$\endgroup\$