Crypto.js 3.9 KB

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