base64.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /****************************************************************
  2. ** Licensed Materials - Property of IBM
  3. ** IBM Cognos Products: CAMCrypto
  4. ** (C) Copyright IBM Corp. 2005, 2012
  5. ** US Government Users Restricted Rights - Use, duplication or
  6. ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. ********************************************************************/
  8. // This code was written by Tyler Akins and has been placed in the
  9. // public domain. It would be nice if you left this header intact.
  10. // Base64 code from Tyler Akins -- http://rumkin.com
  11. function Base64() {
  12. // Note: no return statement here
  13. };
  14. Base64.prototype.encode64 = function( input ) {
  15. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  16. var output = "";
  17. var chr1, chr2, chr3;
  18. var enc1, enc2, enc3, enc4;
  19. var i = 0;
  20. do {
  21. chr1 = input.charCodeAt(i++);
  22. chr2 = input.charCodeAt(i++);
  23. chr3 = input.charCodeAt(i++);
  24. enc1 = chr1 >> 2;
  25. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  26. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  27. enc4 = chr3 & 63;
  28. if (isNaN(chr2)) {
  29. enc3 = enc4 = 64;
  30. } else if (isNaN(chr3)) {
  31. enc4 = 64;
  32. }
  33. output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
  34. keyStr.charAt(enc3) + keyStr.charAt(enc4);
  35. } while (i < input.length);
  36. return output;
  37. }
  38. Base64.prototype.decode64 = function( input ) {
  39. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  40. var output = "";
  41. var chr1, chr2, chr3;
  42. var enc1, enc2, enc3, enc4;
  43. var i = 0;
  44. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  45. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  46. do {
  47. enc1 = keyStr.indexOf(input.charAt(i++));
  48. enc2 = keyStr.indexOf(input.charAt(i++));
  49. enc3 = keyStr.indexOf(input.charAt(i++));
  50. enc4 = keyStr.indexOf(input.charAt(i++));
  51. chr1 = (enc1 << 2) | (enc2 >> 4);
  52. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  53. chr3 = ((enc3 & 3) << 6) | enc4;
  54. output = output + String.fromCharCode(chr1);
  55. if (enc3 != 64) {
  56. output = output + String.fromCharCode(chr2);
  57. }
  58. if (enc4 != 64) {
  59. output = output + String.fromCharCode(chr3);
  60. }
  61. } while (i < input.length);
  62. return output;
  63. }