Javascript password generator


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML><HEAD><TITLE>Javascript password generator</TITLE>
  3. <!-- Password generator originally from : http://motdepasse.site.voila.fr/index.html -->
  4. <SCRIPT language=JavaScript type="text/javascript">
  5.  
  6. /*
  7.  * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  8.  * in FIPS PUB 180-1
  9.  * Version 2.1 Copyright Paul Johnston 2000 - 2002.
  10.  * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  11.  * Distributed under the BSD License
  12.  * See http://pajhome.org.uk/crypt/md5 for details.
  13.  */
  14.  
  15. /*
  16.  * Configurable variables. You may need to tweak these to be compatible with
  17.  * the server-side, but the defaults work in most cases.
  18.  */
  19. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  20. var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  21. var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  22.  
  23. /*
  24.  * These are the functions you'll usually want to call
  25.  * They take string arguments and return either hex or base-64 encoded strings
  26.  */
  27. function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
  28. function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
  29. function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
  30. function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
  31. function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
  32. function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
  33.  
  34. /*
  35.  * Perform a simple self-test to see if the VM is working
  36.  */
  37. function sha1_vm_test()
  38. {
  39. return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
  40. }
  41.  
  42. /*
  43.  * Calculate the SHA-1 of an array of big-endian words, and a bit length
  44.  */
  45. function core_sha1(x, len)
  46. {
  47. /* append padding */
  48. x[len >> 5] |= 0x80 << (24 - len % 32);
  49. x[((len + 64 >> 9) << 4) + 15] = len;
  50.  
  51. var w = Array(80);
  52. var a = 1732584193;
  53. var b = -271733879;
  54. var c = -1732584194;
  55. var d = 271733878;
  56. var e = -1009589776;
  57.  
  58. for(var i = 0; i < x.length; i += 16)
  59. {
  60. var olda = a;
  61. var oldb = b;
  62. var oldc = c;
  63. var oldd = d;
  64. var olde = e;
  65.  
  66. for(var j = 0; j < 80; j++)
  67. {
  68. if(j < 16) w[j] = x[i + j];
  69. else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  70. var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
  71. safe_add(safe_add(e, w[j]), sha1_kt(j)));
  72. e = d;
  73. d = c;
  74. c = rol(b, 30);
  75. b = a;
  76. a = t;
  77. }
  78.  
  79. a = safe_add(a, olda);
  80. b = safe_add(b, oldb);
  81. c = safe_add(c, oldc);
  82. d = safe_add(d, oldd);
  83. e = safe_add(e, olde);
  84. }
  85. return Array(a, b, c, d, e);
  86.  
  87. }
  88.  
  89. /*
  90.  * Perform the appropriate triplet combination function for the current
  91.  * iteration
  92.  */
  93. function sha1_ft(t, b, c, d)
  94. {
  95. if(t < 20) return (b & c) | ((~b) & d);
  96. if(t < 40) return b ^ c ^ d;
  97. if(t < 60) return (b & c) | (b & d) | (c & d);
  98. return b ^ c ^ d;
  99. }
  100.  
  101. /*
  102.  * Determine the appropriate additive constant for the current iteration
  103.  */
  104. function sha1_kt(t)
  105. {
  106. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
  107. (t < 60) ? -1894007588 : -899497514;
  108. }
  109.  
  110. /*
  111.  * Calculate the HMAC-SHA1 of a key and some data
  112.  */
  113. function core_hmac_sha1(key, data)
  114. {
  115. var bkey = str2binb(key);
  116. if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
  117.  
  118. var ipad = Array(16), opad = Array(16);
  119. for(var i = 0; i < 16; i++)
  120. {
  121. ipad[i] = bkey[i] ^ 0x36363636;
  122. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  123. }
  124.  
  125. var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
  126. return core_sha1(opad.concat(hash), 512 + 160);
  127. }
  128.  
  129. /*
  130.  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  131.  * to work around bugs in some JS interpreters.
  132.  */
  133. function safe_add(x, y)
  134. {
  135. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  136. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  137. return (msw << 16) | (lsw & 0xFFFF);
  138. }
  139.  
  140. /*
  141.  * Bitwise rotate a 32-bit number to the left.
  142.  */
  143. function rol(num, cnt)
  144. {
  145. return (num << cnt) | (num >>> (32 - cnt));
  146. }
  147.  
  148. /*
  149.  * Convert an 8-bit or 16-bit string to an array of big-endian words
  150.  * In 8-bit function, characters >255 have their hi-byte silently ignored.
  151.  */
  152. function str2binb(str)
  153. {
  154. var bin = Array();
  155. var mask = (1 << chrsz) - 1;
  156. for(var i = 0; i < str.length * chrsz; i += chrsz)
  157. bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32);
  158. return bin;
  159. }
  160.  
  161. /*
  162.  * Convert an array of big-endian words to a string
  163.  */
  164. function binb2str(bin)
  165. {
  166. var str = "";
  167. var mask = (1 << chrsz) - 1;
  168. for(var i = 0; i < bin.length * 32; i += chrsz)
  169. str += String.fromCharCode((bin[i>>5] >>> (24 - i%32)) & mask);
  170. return str;
  171. }
  172.  
  173. /*
  174.  * Convert an array of big-endian words to a hex string.
  175.  */
  176. function binb2hex(binarray)
  177. {
  178. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  179. var str = "";
  180. for(var i = 0; i < binarray.length * 4; i++)
  181. {
  182. str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
  183. hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
  184. }
  185. return str;
  186. }
  187.  
  188. /*
  189.  * Convert an array of big-endian words to a base-64 string
  190.  */
  191. function binb2b64(binarray)
  192. {
  193. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  194. var str = "";
  195. for(var i = 0; i < binarray.length * 4; i += 3)
  196. {
  197. var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
  198. | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
  199. | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
  200. for(var j = 0; j < 4; j++)
  201. {
  202. if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
  203. else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
  204. }
  205. }
  206. return str;
  207. }
  208.  
  209. function update()
  210. {
  211. document.f.result.value =
  212. b64_hmac_sha1
  213. (document.f.seed.value,
  214. document.f.parameter.value)
  215. .substr(0,8);
  216. }
  217.  
  218. </SCRIPT>
  219.  
  220. </HEAD>
  221. <BODY>
  222. <H1>Javascript password generator</H1>
  223. <FORM name=f action="javascript:update()">
  224. <TABLE>
  225. <TBODY>
  226. <TR>
  227. <TD>Parameter</TD>
  228. <TD><INPUT name=parameter style="font-family: monospace"></TD></TR>
  229. <TR>
  230. <TD>Master password</TD>
  231. <TD><INPUT type=password name=seed style="font-family: monospace"></TD></TR>
  232. <TR>
  233. <TD colSpan=2>
  234. <HR>
  235. </TD></TR>
  236. <TR>
  237. <TD>Variant password</TD>
  238. <TD><INPUT name=result style="font-family: monospace"> <INPUT onclick=update() type=submit value=Update>
  239. </TD></TR></TBODY></TABLE></FORM>
  240. <P>
  241. This form can be used for deriving passwords from a master password and a parameter.
  242. </P>
  243. <p><a href="examples.html">Examples and more info</a></p>
  244. <p><a href="long.html">Long version</a> (do not truncate password to 8 characters, for 160-bit passwords)</p>
  245.  
  246. <script language='javascript'>document.write('<img src="http://monsite.voila.fr/servlets/istat?site=motdepasse&page=/index.html&outil=ftpweb&n=' + Math.round(Math.random()*100000) + '" width=1 height=1>'); </script>
  247. <script language='javascript'>document.write('<img src="http://e.voila.fr/cgi-bin/ft/10000033638?&page=motdepasse_index.html&n=' + Math.round(Math.random()*100000) + '" width=1 height=1>');</script>
  248. </body>
  249. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.