This is a simple version that is hackable but probably one of the best ways of doing this on the client side (another solution is using RSA with JS but that is costly on the cpu. About 30 seconds worth for a simple password).
HOWEVER!!!
Passwords should be on the server side only as this is uncontested by anybody that knows anything about security.
If you have your passwords like this on sent to the client side it would take me about 1 hour to crack one password. It slows me down but it does not stop me.
This type of encryption is good if you want to check that no bad words are being sent in a form element but that is about the only usage I would have for it.
function encrypt(password)
{
var checkSum = 0;
var multiplier = 1;
for (var i = 0; i < password.length; i++)
{
checkSum += (password.charCodeAt(i) * multiplier);
multiplier *= 3;
}
return checkSum;
}
function checkEncrypt(password, encryptedPassword)
{
return (encryptedPassword == encrypt(password))
}
var passwords = [4687]; // this is an array containing only my name "Gary"
alert(checkEncrypt("gary", passwords[0]))
Don't sue me if this gets hacked.
By the way I need to mention this script is based on trollacious's post thread216-30164 Gary
Haran