Assignment 10
Indexes
by zazagalaxy
HTML
<h1>
Password Hashing
</h1>
<h2>Assignment 10</h2>
<form id="form">
<div id="my-div">
Password to List<br>
<input type="text" id="addPassword" value=""><br>
<input type="button" value="Submit" id="add"><br>
Check for Password<br>
<input type="text" id="chkPassword" value="" /><br>
<input type="button" value="Submit" id="check" />
</div>
</form>
<div id="output"></div>
CSS
#add,
#check {
background-color: maroon;
color: black;
margin-top: 10px;
margin-bottom: 10px;
width: 12em;
}
JavaScript
document.getElementById('check').onclick = function() {
var input = document.getElementById("chkPassword").value;
list.search(hashCode(input)); //hash the input taken from the user
}
function List() {
this.head = null;
this.length = 0;
}
function Node(_content) {
this.content = _content;
this.next = null;
}
List.prototype.push = function(_content) {
var node = new Node(_content);
if (!this.head) {
this.head = node;
this.length++;
} else {
var current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
this.length++;
}
}
List.prototype.pop = function() {
if (this.head) {
var itemToPop = this.head;
this.head = this.head.next;
this.length--;
return itemToPop.content;
} else {
return false;
}
}
List.prototype.search = function(_item) {
let current = this.head;
while (current) {
if (current.content == _item) {
document.getElementById('output').innerHTML = "Password found";
return true;
} else {
document.getElementById('output').innerHTML = "Password not found";
return false;
}
current = current.next;
}
}
// Initialize new linked list
var list = new List();
function hashCode(str) {
var hash = 0;
for (let i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
//This function will take user input and take the hash value of it
// and push it to the list.Storing only the hashed value, and not
// not the password itself
document.getElementById('add').onclick = function() {
var uInput = document.getElementById("addPassword").value;
list.push(hashCode(uInput));
}