crypto.js 4.2 KB

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