You can use this hashing function, from this Stack Overflow page:
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
To use it, you first generate the hash of the password:
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
document.querySelector('button').addEventListener('click', e => {
console.log(document.querySelector('input#password').value.hashCode());
});
<input id="password" placeholder="Enter the password to hash">
<button>Hash</button>
Then, once you have the hash for the password you want, you store that hash in your pass1 variable. You also need to hash the user's input and compare it to the hash in pass1.
So your code would look like this:
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash;
};
pass1 = 99162322; // in this case, the password is hello
document.querySelector('button').addEventListener('click', e => {
console.log(document.querySelector('input#password').value.hashCode() == pass1? 'Correct!' : 'Incorrect');
});
<input id="password" placeholder="Enter the password to hash">
<button>Test</button>