A10 Password Hashing

by scotp71

HTML

<h1>
Password Hashing
</h1>
<br/>

<input type="textbox" id="p" value="Add Password To List" />
<input type="button" value="Submit" onclick="addPassword()"/>

<br/>
<br/>

<input type="textbox" id="cp" value="Check For Password" />
<input type="button" value="Submit" onclick="checkPassword()"/>


<div id="output">
</div>

JavaScript

function addPassword(){
	var p = document.getElementById("p").value;
	aList.add(createHash(p));
}

function createHash(_content){
	var M = 128;
	var ch = _content;
  var len = ch.length;
  var num = 0;
  var sum = 0;
  for (var i=0; i<len; i++){
  	num = ch.charCodeAt(i);
    sum += num;
  }
	var key = sum % M;
	return key;
}


function checkPassword(){
	var checkP = createHash(document.getElementById("cp").value);
  var text = "Not Found";
  var node = new Node();
  node = aList.head;
  if(node == null){
  	document.getElementById("output").innerHTML = text;
  }
  for (var i=0; i<aList.length; i++){
  	if (checkP == node.content){
    	text = "Password Found.";
      document.getElementById("output").innerHTML = text;
      return;
      
    }
    if (checkP != node.content){
    	node = node.next;
    }
  }
  document.getElementById("output").innerHTML = text;
  return;
}




function LinkedList(){
	this.head = null;
  this.tail = null;
  this.length = 0;
}

function Node(){
	this.next = null;
  this.prev = null;
  this.content = null;
}

LinkedList.prototype.add = function(_content) {
	var node = new Node();  node.content = _content;
  if (this.head == null) {
  	this.head = node; this.length = 1;
    return node;
  }
  
  if (this.tail == null) {
  	this.tail = node;
    this.tail.prev = this.head;
    this.head.next = this.tail;
    this.length = 2;
    return node;
  }
	
	this.tail.next = node; 
  node.prev = this.tail;
  this.tail = node;
  this.length++;
  return node;
}

LinkedList.prototype.print = function() {
	if (this.head == null) return "Empty List"
  var s = "";
  var node = this.head;
  while (node != null) {
  	s += node.content + " : ";
    node = node.next;
  }
	return s;
}

var aList = new LinkedList();

function addNode() {
	var c = document.getElementById("v").value;
  aList.add(c);
  document.getElementById("output").innerHTML = aList.print();
}