crypto.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. (function() {
  2. Window.Crypto = {};
  3. var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4. // Crypto utilities
  5. var util = Window.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. Window.Crypto.HMAC = function(hasher, message, key, options) {
  139. // Allow arbitrary length keys
  140. key = key.length > hasher._blocksize * 4 ?
  141. hasher(key, {
  142. asBytes: true
  143. }) :
  144. util.stringToBytes(key);
  145. // XOR keys with pad constants
  146. var okey = key,
  147. ikey = key.slice(0);
  148. for (var i = 0; i < hasher._blocksize * 4; i++) {
  149. okey[i] ^= 0x5C;
  150. ikey[i] ^= 0x36;
  151. }
  152. var hmacbytes = hasher(util.bytesToString(okey) +
  153. hasher(util.bytesToString(ikey) + message, {
  154. asString: true
  155. }), {
  156. asBytes: true
  157. });
  158. return options && options.asBytes ? hmacbytes :
  159. options && options.asString ? util.bytesToString(hmacbytes) :
  160. util.bytesToHex(hmacbytes);
  161. };
  162. // The core
  163. var SHA1 = Window.Crypto.SHA1 = function (message, options) {
  164. var digestbytes = util.wordsToBytes(SHA1._sha1(message));
  165. return options && options.asBytes ? digestbytes :
  166. options && options.asString ? util.bytesToString(digestbytes) :
  167. util.bytesToHex(digestbytes);
  168. };
  169. SHA1._sha1 = function(message) {
  170. var m = util.stringToWords(message),
  171. l = message.length * 8,
  172. w = [],
  173. H0 = 1732584193,
  174. H1 = -271733879,
  175. H2 = -1732584194,
  176. H3 = 271733878,
  177. H4 = -1009589776;
  178. // Padding
  179. m[l >> 5] |= 0x80 << (24 - l % 32);
  180. m[((l + 64 >>> 9) << 4) + 15] = l;
  181. for (var i = 0; i < m.length; i += 16) {
  182. var a = H0,
  183. b = H1,
  184. c = H2,
  185. d = H3,
  186. e = H4;
  187. for (var j = 0; j < 80; j++) {
  188. if (j < 16) w[j] = m[i + j];
  189. else {
  190. var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16];
  191. w[j] = (n << 1) | (n >>> 31);
  192. }
  193. var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + (
  194. j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 :
  195. j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 :
  196. j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 :
  197. (H1 ^ H2 ^ H3) - 899497514);
  198. H4 = H3;
  199. H3 = H2;
  200. H2 = (H1 << 30) | (H1 >>> 2);
  201. H1 = H0;
  202. H0 = t;
  203. }
  204. H0 += a;
  205. H1 += b;
  206. H2 += c;
  207. H3 += d;
  208. H4 += e;
  209. }
  210. return [H0, H1, H2, H3, H4];
  211. };
  212. // Package private blocksize
  213. SHA1._blocksize = 16;
  214. // Crypto mode namespace
  215. Window.Crypto.mode = {};
  216. console.log("Window.Crypto")
  217. console.log(Window.Crypto)
  218. return Window.Crypto;
  219. })();