/projekty/list/files/resources/lib/base/jquery.base64.js

https://bitbucket.org/baniol/mobilang · JavaScript · 142 lines · 92 code · 6 blank · 44 comment · 21 complexity · 98eb56cdbfd5fd99183cc77fa964d3c3 MD5 · raw file

  1. /**
  2. * jQuery BASE64 functions
  3. *
  4. * <code>
  5. * Encodes the given data with base64.
  6. * String $.base64Encode ( String str )
  7. * <br />
  8. * Decodes a base64 encoded data.
  9. * String $.base64Decode ( String str )
  10. * </code>
  11. *
  12. * Encodes and Decodes the given data in base64.
  13. * This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.
  14. * Base64-encoded data takes about 33% more space than the original data.
  15. * This javascript code is used to encode / decode data using base64 (this encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean). Script is fully compatible with UTF-8 encoding. You can use base64 encoded data as simple encryption mechanism.
  16. * If you plan using UTF-8 encoding in your project don't forget to set the page encoding to UTF-8 (Content-Type meta tag).
  17. * This function orginally get from the WebToolkit and rewrite for using as the jQuery plugin.
  18. *
  19. * Example
  20. * Code
  21. * <code>
  22. * $.base64Encode("I'm Persian.");
  23. * </code>
  24. * Result
  25. * <code>
  26. * "SSdtIFBlcnNpYW4u"
  27. * </code>
  28. * Code
  29. * <code>
  30. * $.base64Decode("SSdtIFBlcnNpYW4u");
  31. * </code>
  32. * Result
  33. * <code>
  34. * "I'm Persian."
  35. * </code>
  36. *
  37. * @alias Muhammad Hussein Fattahizadeh < muhammad [AT] semnanweb [DOT] com >
  38. * @link http://www.semnanweb.com/jquery-plugin/base64.html
  39. * @see http://www.webtoolkit.info/
  40. * @license http://www.gnu.org/licenses/gpl.html [GNU General Public License]
  41. * @param {jQuery} {base64Encode:function(input))
  42. * @param {jQuery} {base64Decode:function(input))
  43. * @return string
  44. */
  45. (function($){
  46. var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  47. var uTF8Encode = function(string) {
  48. string = string.replace(/\x0d\x0a/g, "\x0a");
  49. var output = "";
  50. for (var n = 0; n < string.length; n++) {
  51. var c = string.charCodeAt(n);
  52. if (c < 128) {
  53. output += String.fromCharCode(c);
  54. } else if ((c > 127) && (c < 2048)) {
  55. output += String.fromCharCode((c >> 6) | 192);
  56. output += String.fromCharCode((c & 63) | 128);
  57. } else {
  58. output += String.fromCharCode((c >> 12) | 224);
  59. output += String.fromCharCode(((c >> 6) & 63) | 128);
  60. output += String.fromCharCode((c & 63) | 128);
  61. }
  62. }
  63. return output;
  64. };
  65. var uTF8Decode = function(input) {
  66. var string = "";
  67. var i = 0;
  68. var c = c1 = c2 = 0;
  69. while ( i < input.length ) {
  70. c = input.charCodeAt(i);
  71. if (c < 128) {
  72. string += String.fromCharCode(c);
  73. i++;
  74. } else if ((c > 191) && (c < 224)) {
  75. c2 = input.charCodeAt(i+1);
  76. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  77. i += 2;
  78. } else {
  79. c2 = input.charCodeAt(i+1);
  80. c3 = input.charCodeAt(i+2);
  81. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  82. i += 3;
  83. }
  84. }
  85. return string;
  86. }
  87. $.extend({
  88. base64Encode: function(input) {
  89. var output = "";
  90. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  91. var i = 0;
  92. input = uTF8Encode(input);
  93. while (i < input.length) {
  94. chr1 = input.charCodeAt(i++);
  95. chr2 = input.charCodeAt(i++);
  96. chr3 = input.charCodeAt(i++);
  97. enc1 = chr1 >> 2;
  98. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  99. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  100. enc4 = chr3 & 63;
  101. if (isNaN(chr2)) {
  102. enc3 = enc4 = 64;
  103. } else if (isNaN(chr3)) {
  104. enc4 = 64;
  105. }
  106. output = output + keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4);
  107. }
  108. return output;
  109. },
  110. base64Decode: function(input) {
  111. var output = "";
  112. var chr1, chr2, chr3;
  113. var enc1, enc2, enc3, enc4;
  114. var i = 0;
  115. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  116. while (i < input.length) {
  117. enc1 = keyString.indexOf(input.charAt(i++));
  118. enc2 = keyString.indexOf(input.charAt(i++));
  119. enc3 = keyString.indexOf(input.charAt(i++));
  120. enc4 = keyString.indexOf(input.charAt(i++));
  121. chr1 = (enc1 << 2) | (enc2 >> 4);
  122. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  123. chr3 = ((enc3 & 3) << 6) | enc4;
  124. output = output + String.fromCharCode(chr1);
  125. if (enc3 != 64) {
  126. output = output + String.fromCharCode(chr2);
  127. }
  128. if (enc4 != 64) {
  129. output = output + String.fromCharCode(chr3);
  130. }
  131. }
  132. output = uTF8Decode(output);
  133. return output;
  134. }
  135. });
  136. })(jQuery);