PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/camila/js/login_storepwd.js

https://github.com/umbecr/camilaframework
JavaScript | 168 lines | 96 code | 21 blank | 51 comment | 13 complexity | 90132c58f9b149b9888d54f251700e3b MD5 | raw file
  1. /* This File is part of Camila PHP Framework
  2. Copyright (C) 2006-2008 Umberto Bresciani
  3. Camila PHP Framework is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. Camila PHP Framework is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Camila PHP Framework; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  14. // 'Block' Tiny Encryption Algorithm
  15. //
  16. // Algorithm: David Wheeler & Roger Needham, Cambridge University Computer Lab
  17. // http://www.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html (1994)
  18. // http://www.cl.cam.ac.uk/ftp/users/djw3/xtea.ps (1997)
  19. // http://www.cl.cam.ac.uk/ftp/users/djw3/xxtea.ps (1998)
  20. //
  21. // JavaScript implementation: Chris Veness, Movable Type Ltd: www.movable-type.co.uk
  22. //
  23. //
  24. // encrypt: Use 128 bits (16 chars) of string 'key' to encrypt string 'val'
  25. // (note key & val must be strings not string objects)
  26. //
  27. // Return encrypted text as string
  28. //
  29. function encrypt(val, key)
  30. {
  31. // 'escape' val so chars outside ISO-8859-1 work in single-byte packing, but keep spaces as
  32. // spaces (not '%20') so encrypted text doesn't grow too long!
  33. var v = strToLongs(escape(val).replace(/%20/g,' '));
  34. var k = strToLongs(key.slice(0,16));
  35. var n = v.length;
  36. if (n == 0) return(""); // nothing to encrypt
  37. if (n == 1) v[n++] = 0; // algorithm doesn't work for n<2 so fudge by adding nulls
  38. // TEA routine as per Wheeler & Needham, Oct 1998
  39. var z = v[n-1], y = v[0], delta = 0x9E3779B9;
  40. var mx, e, q = Math.floor(6 + 52/n), sum = 0;
  41. while (q-- > 0) { // 6 + 52/n operations gives between 6 & 32 mixes on each word
  42. sum += delta;
  43. e = sum>>>2 & 3;
  44. for (var p = 0; p < n-1; p++) {
  45. y = v[p+1];
  46. mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
  47. z = v[p] += mx;
  48. }
  49. y = v[0];
  50. mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z)
  51. z = v[n-1] += mx;
  52. }
  53. // note: unsigned right-shift '>>>' is used in place of original '>>', due to lack of
  54. // 'unsigned' type declaration in JavaScript (thanks to Karsten Kraus @ swr3 for this)
  55. return esc(longsToStr(v));
  56. }
  57. function strToLongs(s) { // convert string to array of longs, each containing 4 chars
  58. // note chars must be within ISO-8859-1 (with Unicode code-point < 256) to fit 4/long
  59. var l = new Array(Math.ceil(s.length/4))
  60. for (var i=0; i<l.length; i++) {
  61. // note little-endian encoding - endianness is irrelevant as long as
  62. // it is the same in longsToStr()
  63. l[i] = s.charCodeAt(i*4) + (s.charCodeAt(i*4+1)<<8) +
  64. (s.charCodeAt(i*4+2)<<16) + (s.charCodeAt(i*4+3)<<24);
  65. }
  66. return l; // note running off the end of the string generates nulls since
  67. } // bitwise operators treat NaN as 0
  68. function longsToStr(l) { // convert array of longs back to string
  69. var a = new Array(l.length);
  70. for (var i=0; i<l.length; i++) {
  71. a[i] = String.fromCharCode(l[i] & 0xFF, l[i]>>>8 & 0xFF,
  72. l[i]>>>16 & 0xFF, l[i]>>>24 & 0xFF);
  73. }
  74. return a.join(''); // use Array.join() rather than repeated string appends for efficiency
  75. }
  76. function esc(str) { // escape null and control chars which might cause problems in loading encrypted texts
  77. return str.replace(/[\0\n\v\f\r!]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });
  78. }
  79. function unesc(str) { // unescape potentially problematic nulls and control characters
  80. return str.replace(/!\d\d?!/g, function(c) { return String.fromCharCode(c.slice(1,-1)); });
  81. }
  82. /**
  83. * Sets a Cookie with the given name and value.
  84. *
  85. * name Name of the cookie
  86. * value Value of the cookie
  87. * [expires] Expiration date of the cookie (default: end of current session)
  88. * [path] Path where the cookie is valid (default: path of calling document)
  89. * [domain] Domain where the cookie is valid
  90. * (default: domain of calling document)
  91. * [secure] Boolean value indicating if the cookie transmission requires a
  92. * secure transmission
  93. */
  94. function setCookie(name, value, expires, path, domain, secure)
  95. {
  96. document.cookie= name + "=" + escape(value) +
  97. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  98. ((path) ? "; path=" + path : "") +
  99. ((domain) ? "; domain=" + domain : "") +
  100. ((secure) ? "; secure" : "");
  101. }
  102. var keyStr = "ABCDEFGHIJKLMNOP" +
  103. "QRSTUVWXYZabcdef" +
  104. "ghijklmnopqrstuv" +
  105. "wxyz0123456789+/" +
  106. "=";
  107. function encode64(input) {
  108. var output = "";
  109. var chr1, chr2, chr3 = "";
  110. var enc1, enc2, enc3, enc4 = "";
  111. var i = 0;
  112. do {
  113. chr1 = input.charCodeAt(i++);
  114. chr2 = input.charCodeAt(i++);
  115. chr3 = input.charCodeAt(i++);
  116. enc1 = chr1 >> 2;
  117. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  118. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  119. enc4 = chr3 & 63;
  120. if (isNaN(chr2)) {
  121. enc3 = enc4 = 64;
  122. } else if (isNaN(chr3)) {
  123. enc4 = 64;
  124. }
  125. output = output +
  126. keyStr.charAt(enc1) +
  127. keyStr.charAt(enc2) +
  128. keyStr.charAt(enc3) +
  129. keyStr.charAt(enc4);
  130. chr1 = chr2 = chr3 = "";
  131. enc1 = enc2 = enc3 = enc4 = "";
  132. } while (i < input.length);
  133. return output;
  134. }
  135. function store() {
  136. if (!confirm("Verra' ora memorizzato su questo computer il codice numerico\ndi accesso al sistema codificandolo con una password.\nSuccessivamente per accedere bastera' introdurre la password\nscelta al posto del codice numerico.\nProcedere con la memorizzazione?"))
  137. return;
  138. var pass1=prompt("Digitare il codice numerico:","");
  139. var pass2=prompt("Digitare la password:","");
  140. var todayDate = largeExpDate = new Date ();
  141. largeExpDate.setTime(todayDate.getTime() + 365 * 24 * 3600 * 1000);
  142. setCookie("camila_lbox",encode64(encrypt(pass1, pass2)), largeExpDate);
  143. location.reload();
  144. }