crypto.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Crypto-JS v2.5.3
  3. * http://code.google.com/p/crypto-js/
  4. * (c) 2009-2012 by Jeff Mott. All rights reserved.
  5. * http://code.google.com/p/crypto-js/wiki/License
  6. */
  7. if (typeof Crypto == "undefined" || ! Crypto.util)
  8. {
  9. (function(){
  10. var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  11. // Global Crypto object
  12. var Crypto = window.Crypto = {};
  13. // Crypto utilities
  14. var util = Crypto.util = {
  15. // Bit-wise rotate left
  16. rotl: function (n, b) {
  17. return (n << b) | (n >>> (32 - b));
  18. },
  19. // Bit-wise rotate right
  20. rotr: function (n, b) {
  21. return (n << (32 - b)) | (n >>> b);
  22. },
  23. // Swap big-endian to little-endian and vice versa
  24. endian: function (n) {
  25. // If number given, swap endian
  26. if (n.constructor == Number) {
  27. return util.rotl(n, 8) & 0x00FF00FF |
  28. util.rotl(n, 24) & 0xFF00FF00;
  29. }
  30. // Else, assume array and swap all items
  31. for (var i = 0; i < n.length; i++)
  32. n[i] = util.endian(n[i]);
  33. return n;
  34. },
  35. // Generate an array of any length of random bytes
  36. randomBytes: function (n) {
  37. for (var bytes = []; n > 0; n--)
  38. bytes.push(Math.floor(Math.random() * 256));
  39. return bytes;
  40. },
  41. // Convert a byte array to big-endian 32-bit words
  42. bytesToWords: function (bytes) {
  43. for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8)
  44. words[b >>> 5] |= (bytes[i] & 0xFF) << (24 - b % 32);
  45. return words;
  46. },
  47. // Convert big-endian 32-bit words to a byte array
  48. wordsToBytes: function (words) {
  49. for (var bytes = [], b = 0; b < words.length * 32; b += 8)
  50. bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
  51. return bytes;
  52. },
  53. // Convert a byte array to a hex string
  54. bytesToHex: function (bytes) {
  55. for (var hex = [], i = 0; i < bytes.length; i++) {
  56. hex.push((bytes[i] >>> 4).toString(16));
  57. hex.push((bytes[i] & 0xF).toString(16));
  58. }
  59. return hex.join("");
  60. },
  61. // Convert a hex string to a byte array
  62. hexToBytes: function (hex) {
  63. for (var bytes = [], c = 0; c < hex.length; c += 2)
  64. bytes.push(parseInt(hex.substr(c, 2), 16));
  65. return bytes;
  66. },
  67. // Convert a byte array to a base-64 string
  68. bytesToBase64: function (bytes) {
  69. // Use browser-native function if it exists
  70. if (typeof btoa == "function") return btoa(Binary.bytesToString(bytes));
  71. for(var base64 = [], i = 0; i < bytes.length; i += 3) {
  72. var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
  73. for (var j = 0; j < 4; j++) {
  74. if (i * 8 + j * 6 <= bytes.length * 8)
  75. base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
  76. else base64.push("=");
  77. }
  78. }
  79. return base64.join("");
  80. },
  81. // Convert a base-64 string to a byte array
  82. base64ToBytes: function (base64) {
  83. // Use browser-native function if it exists
  84. if (typeof atob == "function") return Binary.stringToBytes(atob(base64));
  85. // Remove non-base-64 characters
  86. base64 = base64.replace(/[^A-Z0-9+\/]/ig, "");
  87. for (var bytes = [], i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) {
  88. if (imod4 == 0) continue;
  89. bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) |
  90. (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)));
  91. }
  92. return bytes;
  93. }
  94. };
  95. // Crypto character encodings
  96. var charenc = Crypto.charenc = {};
  97. // UTF-8 encoding
  98. var UTF8 = charenc.UTF8 = {
  99. // Convert a string to a byte array
  100. stringToBytes: function (str) {
  101. return Binary.stringToBytes(unescape(encodeURIComponent(str)));
  102. },
  103. // Convert a byte array to a string
  104. bytesToString: function (bytes) {
  105. return decodeURIComponent(escape(Binary.bytesToString(bytes)));
  106. }
  107. };
  108. // Binary encoding
  109. var Binary = charenc.Binary = {
  110. // Convert a string to a byte array
  111. stringToBytes: function (str) {
  112. for (var bytes = [], i = 0; i < str.length; i++)
  113. bytes.push(str.charCodeAt(i) & 0xFF);
  114. return bytes;
  115. },
  116. // Convert a byte array to a string
  117. bytesToString: function (bytes) {
  118. for (var str = [], i = 0; i < bytes.length; i++)
  119. str.push(String.fromCharCode(bytes[i]));
  120. return str.join("");
  121. }
  122. };
  123. })();
  124. }