排队支付小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
6.5 KiB

  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. var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  8. // Global Crypto object
  9. var Crypto = {};
  10. // Crypto utilities
  11. var util = Crypto.util = {
  12. // Bit-wise rotate left
  13. rotl: function (n, b) {
  14. return (n << b) | (n >>> (32 - b));
  15. },
  16. // Bit-wise rotate right
  17. rotr: function (n, b) {
  18. return (n << (32 - b)) | (n >>> b);
  19. },
  20. // Swap big-endian to little-endian and vice versa
  21. endian: function (n) {
  22. // If number given, swap endian
  23. if (n.constructor == Number) {
  24. return util.rotl(n, 8) & 0x00FF00FF |
  25. util.rotl(n, 24) & 0xFF00FF00;
  26. }
  27. // Else, assume array and swap all items
  28. for (var i = 0; i < n.length; i++)
  29. n[i] = util.endian(n[i]);
  30. return n;
  31. },
  32. // Generate an array of any length of random bytes
  33. randomBytes: function (n) {
  34. for (var bytes = []; n > 0; n--)
  35. bytes.push(Math.floor(Math.random() * 256));
  36. return bytes;
  37. },
  38. // Convert a string to a byte array
  39. stringToBytes: function (str) {
  40. var bytes = [];
  41. for (var i = 0; i < str.length; i++)
  42. bytes.push(str.charCodeAt(i));
  43. return bytes;
  44. },
  45. // Convert a byte array to a string
  46. bytesToString: function (bytes) {
  47. var str = [];
  48. for (var i = 0; i < bytes.length; i++)
  49. str.push(String.fromCharCode(bytes[i]));
  50. return str.join("");
  51. },
  52. // Convert a string to big-endian 32-bit words
  53. stringToWords: function (str) {
  54. var words = [];
  55. for (var c = 0, b = 0; c < str.length; c++, b += 8)
  56. words[b >>> 5] |= str.charCodeAt(c) << (24 - b % 32);
  57. return words;
  58. },
  59. // Convert a byte array to big-endian 32-bits words
  60. bytesToWords: function (bytes) {
  61. var words = [];
  62. for (var i = 0, b = 0; i < bytes.length; i++, b += 8)
  63. words[b >>> 5] |= bytes[i] << (24 - b % 32);
  64. return words;
  65. },
  66. // Convert big-endian 32-bit words to a byte array
  67. wordsToBytes: function (words) {
  68. var bytes = [];
  69. for (var b = 0; b < words.length * 32; b += 8)
  70. bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
  71. return bytes;
  72. },
  73. // Convert a byte array to a hex string
  74. bytesToHex: function (bytes) {
  75. var hex = [];
  76. for (var i = 0; i < bytes.length; i++) {
  77. hex.push((bytes[i] >>> 4).toString(16));
  78. hex.push((bytes[i] & 0xF).toString(16));
  79. }
  80. return hex.join("");
  81. },
  82. // Convert a hex string to a byte array
  83. hexToBytes: function (hex) {
  84. var bytes = [];
  85. for (var c = 0; c < hex.length; c += 2)
  86. bytes.push(parseInt(hex.substr(c, 2), 16));
  87. return bytes;
  88. },
  89. // Convert a byte array to a base-64 string
  90. bytesToBase64: function (bytes) {
  91. // Use browser-native function if it exists
  92. if (typeof btoa == "function") return btoa(util.bytesToString(bytes));
  93. var base64 = [],
  94. overflow;
  95. for (var i = 0; i < bytes.length; i++) {
  96. switch (i % 3) {
  97. case 0:
  98. base64.push(base64map.charAt(bytes[i] >>> 2));
  99. overflow = (bytes[i] & 0x3) << 4;
  100. break;
  101. case 1:
  102. base64.push(base64map.charAt(overflow | (bytes[i] >>> 4)));
  103. overflow = (bytes[i] & 0xF) << 2;
  104. break;
  105. case 2:
  106. base64.push(base64map.charAt(overflow | (bytes[i] >>> 6)));
  107. base64.push(base64map.charAt(bytes[i] & 0x3F));
  108. overflow = -1;
  109. }
  110. }
  111. // Encode overflow bits, if there are any
  112. if (overflow != undefined && overflow != -1)
  113. base64.push(base64map.charAt(overflow));
  114. // Add padding
  115. while (base64.length % 4 != 0) base64.push("=");
  116. return base64.join("");
  117. },
  118. // Convert a base-64 string to a byte array
  119. base64ToBytes: function (base64) {
  120. // Use browser-native function if it exists
  121. if (typeof atob == "function") return util.stringToBytes(atob(base64));
  122. // Remove non-base-64 characters
  123. base64 = base64.replace(/[^A-Z0-9+\/]/ig, "");
  124. var bytes = [];
  125. for (var i = 0; i < base64.length; i++) {
  126. switch (i % 4) {
  127. case 1:
  128. bytes.push((base64map.indexOf(base64.charAt(i - 1)) << 2) |
  129. (base64map.indexOf(base64.charAt(i)) >>> 4));
  130. break;
  131. case 2:
  132. bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & 0xF) << 4) |
  133. (base64map.indexOf(base64.charAt(i)) >>> 2));
  134. break;
  135. case 3:
  136. bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & 0x3) << 6) |
  137. (base64map.indexOf(base64.charAt(i))));
  138. break;
  139. }
  140. }
  141. return bytes;
  142. }
  143. };
  144. // Crypto mode namespace
  145. Crypto.mode = {};
  146. Crypto.HMAC = function (hasher, message, key, options) {
  147. // Allow arbitrary length keys
  148. key = key.length > hasher._blocksize * 4 ?
  149. hasher(key, { asBytes: true }) :
  150. util.stringToBytes(key);
  151. // XOR keys with pad constants
  152. var okey = key,
  153. ikey = key.slice(0);
  154. for (var i = 0; i < hasher._blocksize * 4; i++) {
  155. okey[i] ^= 0x5C;
  156. ikey[i] ^= 0x36;
  157. }
  158. var hmacbytes = hasher(util.bytesToString(okey) +
  159. hasher(util.bytesToString(ikey) + message, { asString: true }),
  160. { asBytes: true });
  161. return options && options.asBytes ? hmacbytes :
  162. options && options.asString ? util.bytesToString(hmacbytes) :
  163. util.bytesToHex(hmacbytes);
  164. };
  165. // Public API
  166. var SHA1 = Crypto.SHA1 = function (message, options) {
  167. var digestbytes = util.wordsToBytes(SHA1._sha1(message));
  168. return options && options.asBytes ? digestbytes :
  169. options && options.asString ? util.bytesToString(digestbytes) :
  170. util.bytesToHex(digestbytes);
  171. };
  172. // The core
  173. SHA1._sha1 = function (message) {
  174. var m = util.stringToWords(message),
  175. l = message.length * 8,
  176. w = [],
  177. H0 = 1732584193,
  178. H1 = -271733879,
  179. H2 = -1732584194,
  180. H3 = 271733878,
  181. H4 = -1009589776;
  182. // Padding
  183. m[l >> 5] |= 0x80 << (24 - l % 32);
  184. m[((l + 64 >>> 9) << 4) + 15] = l;
  185. for (var i = 0; i < m.length; i += 16) {
  186. var a = H0,
  187. b = H1,
  188. c = H2,
  189. d = H3,
  190. e = H4;
  191. for (var j = 0; j < 80; j++) {
  192. if (j < 16) w[j] = m[i + j];
  193. else {
  194. var n = w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16];
  195. w[j] = (n << 1) | (n >>> 31);
  196. }
  197. var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + (
  198. j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 :
  199. j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 :
  200. j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 :
  201. (H1 ^ H2 ^ H3) - 899497514);
  202. H4 = H3;
  203. H3 = H2;
  204. H2 = (H1 << 30) | (H1 >>> 2);
  205. H1 = H0;
  206. H0 = t;
  207. }
  208. H0 += a;
  209. H1 += b;
  210. H2 += c;
  211. H3 += d;
  212. H4 += e;
  213. }
  214. return [H0, H1, H2, H3, H4];
  215. };
  216. // Package private blocksize
  217. SHA1._blocksize = 16;
  218. module.exports = Crypto;