Assignment 10 Password Hashing

by austinmillett

HTML

<!-- Heading 1 -->
<h2> Austin Millett </h2>

<!-- Heading 2 -->
<h3> Assignment 10 - Password Hashing </h3>

<!-- Textbox 1 -->
Add Password To List:
<input type = "textbox" id = "addPass" />

<!-- Submit button 1 -->
<input type = "button" id = "newPass" value = "Submit" onclick = "addAPassword()" /> <br/> <br/>

<!-- Textbox 2 -->
Check For Password:
<input type = "textbox" id = "checkPass" />

<!-- Submit button 2 -->
<input type = "button"  id = "lookPass" value = "Submit" onclick = "checkForPassword()" /> <br/>

<!-- Div for Output -->
<div id = "output"> </div>

CSS

/* Design for first "Submit" button */
#newPass {
background-color: black; 
border: 2px solid; 
color: white;
padding: 4px 8px; 
text-align: center; 
font-size: 15px; 
}

/* Design for second "Submit" button */
#lookPass {
background-color: black; 
border: 2px solid; 
color: white;
padding: 4px 8px; 
text-align: center; 
font-size: 15px; 
}

JavaScript

// Hashing Function
function simpleHash(str) {
len = str.length;
hash = 0;
for (i = 1; i <= len; i++) {
char = str.charCodeAt((i - 1));
hash += char * Math.pow(31, (len - i));
hash = hash & hash;
}
return hash;
}

// Linked list
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node(element) {
this.next = null;
this.prev = null;
this.item = element;
}

LinkedList.prototype.add = function(info) {
var node = new Node();
node.item = info
if (!this.head) {
this.head = node;
this.tail = node;
} 
else 
{
node.previous = this.tail;
this.tail.next = node;
this.tail = node;
}
this.numberOfValues++;
}

var list = new LinkedList();

// Adding the password to list
function addAPassword() {
var c = document.getElementById("addPass").value;
c = simpleHash(c);
list.add(c);
list.length++;
alert(c);
}

// Checking the list for the added password
function checkForPassword() {
var check = document.getElementById("checkPass").value;
check = simpleHash(check);
alert(check);
alert(list.length);
var len = list.length;

// Searching for passwords if found report if not found report
var node = list.head;
for(var i =0; i < len; i++){
alert(node.item); 
if (check == node.item){
alert("Password Found");
return;
}
else
{
node = node.next;
}
}
alert("Password Not Found");
}