crypto.js 4.6 KB

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