I have a JavaScript Password Generator I need a hand with. I wanted to add a verification of the master password field, Its added and is almost functional. The code is public source (the MD5 under the BSD license), help fix it and you can use it too.
I know my code sucks, I don't do JavaScript.
You'll see the code near the bottom for the form to verify the passwords and generate the concentrated MD5 of the master password and the site/resource fields. I cant get it to verify the passwords and generate the MD5.
<br />
<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"><br />
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><br />
<head><br />
<title><br />
Password Generator <br />
</title><br />
<script language="JavaScript"><br />
<br />
<br />
<br />
<br />
var hexcase = 0; <br />
var chrsz = 8; <br />
<br />
<br />
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}<br />
<br />
<br />
function core_md5(x, len)<br />
{<br />
<br />
x[len >> 5] = 0x80 << ((len) % 32);<br />
x[(((len + 64) >>> 9) << 4) + 14] = len;<br />
<br />
var a = 1732584193;<br />
var b = 271733879;<br />
var c = 1732584194;<br />
var d = 271733878;<br />
<br />
for(var i = 0; i < x.length; i += 16)<br />
{<br />
var olda = a;<br />
var oldb = b;<br />
var oldc = c;<br />
var oldd = d;<br />
<br />
a = md5_ff(a, b, c, d, x[i+ 0], 7 , 680876936);<br />
d = md5_ff(d, a, b, c, x[i+ 1], 12, 389564586);<br />
c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);<br />
b = md5_ff(b, c, d, a, x[i+ 3], 22, 1044525330);<br />
a = md5_ff(a, b, c, d, x[i+ 4], 7 , 176418897);<br />
d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);<br />
c = md5_ff(c, d, a, b, x[i+ 6], 17, 1473231341);<br />
b = md5_ff(b, c, d, a, x[i+ 7], 22, 45705983);<br />
a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);<br />
d = md5_ff(d, a, b, c, x[i+ 9], 12, 1958414417);<br />
c = md5_ff(c, d, a, b, x[i+10], 17, 42063);<br />
b = md5_ff(b, c, d, a, x[i+11], 22, 1990404162);<br />
a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);<br />
d = md5_ff(d, a, b, c, x[i+13], 12, 40341101);<br />
c = md5_ff(c, d, a, b, x[i+14], 17, 1502002290);<br />
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);<br />
<br />
a = md5_gg(a, b, c, d, x[i+ 1], 5 , 165796510);<br />
d = md5_gg(d, a, b, c, x[i+ 6], 9 , 1069501632);<br />
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);<br />
b = md5_gg(b, c, d, a, x[i+ 0], 20, 373897302);<br />
a = md5_gg(a, b, c, d, x[i+ 5], 5 , 701558691);<br />
d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);<br />
c = md5_gg(c, d, a, b, x[i+15], 14, 660478335);<br />
b = md5_gg(b, c, d, a, x[i+ 4], 20, 405537848);<br />
a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);<br />
d = md5_gg(d, a, b, c, x[i+14], 9 , 1019803690);<br />
c = md5_gg(c, d, a, b, x[i+ 3], 14, 187363961);<br />
b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);<br />
a = md5_gg(a, b, c, d, x[i+13], 5 , 1444681467);<br />
d = md5_gg(d, a, b, c, x[i+ 2], 9 , 51403784);<br />
c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);<br />
b = md5_gg(b, c, d, a, x[i+12], 20, 1926607734);<br />
<br />
a = md5_hh(a, b, c, d, x[i+ 5], 4 , 378558);<br />
d = md5_hh(d, a, b, c, x[i+ 8], 11, 2022574463);<br />
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);<br />
b = md5_hh(b, c, d, a, x[i+14], 23, 35309556);<br />
a = md5_hh(a, b, c, d, x[i+ 1], 4 , 1530992060);<br />
d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);<br />
c = md5_hh(c, d, a, b, x[i+ 7], 16, 155497632);<br />
b = md5_hh(b, c, d, a, x[i+10], 23, 1094730640);<br />
a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);<br />
d = md5_hh(d, a, b, c, x[i+ 0], 11, 358537222);<br />
c = md5_hh(c, d, a, b, x[i+ 3], 16, 722521979);<br />
b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);<br />
a = md5_hh(a, b, c, d, x[i+ 9], 4 , 640364487);<br />
d = md5_hh(d, a, b, c, x[i+12], 11, 421815835);<br />
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);<br />
b = md5_hh(b, c, d, a, x[i+ 2], 23, 995338651);<br />
<br />
a = md5_ii(a, b, c, d, x[i+ 0], 6 , 198630844);<br />
d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);<br />
c = md5_ii(c, d, a, b, x[i+14], 15, 1416354905);<br />
b = md5_ii(b, c, d, a, x[i+ 5], 21, 57434055);<br />
a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);<br />
d = md5_ii(d, a, b, c, x[i+ 3], 10, 1894986606);<br />
c = md5_ii(c, d, a, b, x[i+10], 15, 1051523);<br />
b = md5_ii(b, c, d, a, x[i+ 1], 21, 2054922799);<br />
a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);<br />
d = md5_ii(d, a, b, c, x[i+15], 10, 30611744);<br />
c = md5_ii(c, d, a, b, x[i+ 6], 15, 1560198380);<br />
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);<br />
a = md5_ii(a, b, c, d, x[i+ 4], 6 , 145523070);<br />
d = md5_ii(d, a, b, c, x[i+11], 10, 1120210379);<br />
c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);<br />
b = md5_ii(b, c, d, a, x[i+ 9], 21, 343485551);<br />
<br />
a = safe_add(a, olda);<br />
b = safe_add(b, oldb);<br />
c = safe_add(c, oldc);<br />
d = safe_add(d, oldd);<br />
}<br />
return Array(a, b, c, d);<br />
<br />
}<br />
<br />
<br />
function md5_cmn(q, a, b, x, s, t)<br />
{<br />
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);<br />
}<br />
function md5_ff(a, b, c, d, x, s, t)<br />
{<br />
return md5_cmn((b & c)  ((~b) & d), a, b, x, s, t);<br />
}<br />
function md5_gg(a, b, c, d, x, s, t)<br />
{<br />
return md5_cmn((b & d)  (c & (~d)), a, b, x, s, t);<br />
}<br />
function md5_hh(a, b, c, d, x, s, t)<br />
{<br />
return md5_cmn(b ^ c ^ d, a, b, x, s, t);<br />
}<br />
function md5_ii(a, b, c, d, x, s, t)<br />
{<br />
return md5_cmn(c ^ (b  (~d)), a, b, x, s, t);<br />
}<br />
<br />
<br />
function safe_add(x, y)<br />
{<br />
var lsw = (x & 0xFFFF) + (y & 0xFFFF);<br />
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);<br />
return (msw << 16)  (lsw & 0xFFFF);<br />
}<br />
<br />
<br />
function bit_rol(num, cnt)<br />
{<br />
return (num << cnt)  (num >>> (32  cnt));<br />
}<br />
<br />
<br />
function str2binl(str)<br />
{<br />
var bin = Array();<br />
var mask = (1 << chrsz)  1;<br />
for(var i = 0; i < str.length * chrsz; i += chrsz)<br />
bin[i>>5] = (str.charCodeAt(i / chrsz) & mask) << (i%32);<br />
return bin;<br />
}<br />
<br />
<br />
function binl2hex(binarray)<br />
{<br />
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";<br />
var str = "";<br />
for(var i = 0; i < binarray.length * 4; i++)<br />
{<br />
str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +<br />
hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);<br />
}<br />
return str;<br />
}<br />
<br />
</script><br />
</head><br />
<body><br />
<! Start of Master Password Verify and Submit Form ><br />
<script><br />
var fieldalias="Master Password"<br />
<br />
function verify(element1, element2){<br />
var passed=false<br />
if (element1.value==''){<br />
alert("Please fill out the "Master Password")<br />
element1.focus()<br />
}<br />
else if (element2.value==''){<br />
alert("Please verify the Master Password")<br />
element2.focus()<br />
}<br />
else if (element1.value!=element2.value){<br />
alert("The two Master Passwords do not match")<br />
element1.select()<br />
}<br />
else<br />
passed=true<br />
<br />
return passed<br />
<br />
}<br />
<br />
</script><br />
<!Below is my bad code :D ><br />
<form class="callout" name="f" onSubmit="return verify(this.master, this.vmaster);password.value = hex_md5(master.value + <br />
<br />
':' + site.value).substr(0,8);document.f.password.focus();document.f.password.select();return false"><br />
<br />
<table><br />
<tr><br />
<td>Your Master Password: <input name="master" type="password"></td><br />
<script language="JavaScript">document.f.master.focus()</script><br />
</tr><br />
<tr><br />
<td>Verify Master Password: <input name="vmaster" type="password"></td><br />
</tr><br />
<tr><br />
<td>Site/Resource Name: </td><br />
<td><input name="site" /></td><br />
</tr><br />
<tr><br />
<td colspan="2"><input type="submit" value="Generate"><br />
</td><br />
</tr><br />
<tr class="output"><br />
<td>Generated Password: </td><br />
<td><input name="password" /></td><br />
</tr><br />
</table><br />
</form><br />
</body><br />
</html> <br />
