Guest User

Untitled

a guest
Sep 9th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.59 KB | None | 0 0
  1. // Function which creates the Service Bus SAS token.
  2. function getToken (uri,key,keyname) {
  3.  
  4. var endocedResourceUri = encodeURIComponent(uri);
  5.  
  6. var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
  7. var t1 = new Date();
  8. var expireInSeconds = + (31*24*3600) + 3600 + (((t1.getTime() - t0.getTime()) / 1000) | 0);
  9.  
  10. var plainSignature = utf8_encode(endocedResourceUri + "\n" + expireInSeconds);
  11.  
  12. var hash = CryptoJS.HmacSHA256(plainSignature, key);
  13. var base64HashValue = CryptoJS.enc.Base64.stringify(hash);
  14.  
  15. var token = "SharedAccessSignature sr=" + endocedResourceUri + "&sig=" +
  16. encodeURIComponent(base64HashValue) + "&se=" + expireInSeconds + "&skn=" +
  17. keyname;
  18.  
  19. return token;
  20. }
  21.  
  22. function utf8_encode(argString) {
  23. // discuss at: http://phpjs.org/functions/utf8_encode/
  24. // original by: Webtoolkit.info (http://www.webtoolkit.info/)
  25. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  26. // improved by: sowberry
  27. // improved by: Jack
  28. // improved by: Yves Sucaet
  29. // improved by: kirilloid
  30. // bugfixed by: Onno Marsman
  31. // bugfixed by: Onno Marsman
  32. // bugfixed by: Ulrich
  33. // bugfixed by: Rafal Kukawski
  34. // bugfixed by: kirilloid
  35. // example 1: utf8_encode('Kevin van Zonneveld');
  36. // returns 1: 'Kevin van Zonneveld'
  37.  
  38. if (argString === null || typeof argString === 'undefined') {
  39. return '';
  40. }
  41.  
  42. var string = (argString + ''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
  43. var utftext = '',
  44. start, end, stringl = 0;
  45.  
  46. start = end = 0;
  47. stringl = string.length;
  48. for (var n = 0; n < stringl; n++) {
  49. var c1 = string.charCodeAt(n);
  50. var enc = null;
  51.  
  52. if (c1 < 128) {
  53. end++;
  54. } else if (c1 > 127 && c1 < 2048) {
  55. enc = String.fromCharCode(
  56. (c1 >> 6) | 192, (c1 & 63) | 128
  57. );
  58. } else if ((c1 & 0xF800) != 0xD800) {
  59. enc = String.fromCharCode(
  60. (c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128
  61. );
  62. } else { // surrogate pairs
  63. if ((c1 & 0xFC00) != 0xD800) {
  64. throw new RangeError('Unmatched trail surrogate at ' + n);
  65. }
  66. var c2 = string.charCodeAt(++n);
  67. if ((c2 & 0xFC00) != 0xDC00) {
  68. throw new RangeError('Unmatched lead surrogate at ' + (n - 1));
  69. }
  70. c1 = ((c1 & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000;
  71. enc = String.fromCharCode(
  72. (c1 >> 18) | 240, ((c1 >> 12) & 63) | 128, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128
  73. );
  74. }
  75. if (enc !== null) {
  76. if (end > start) {
  77. utftext += string.slice(start, end);
  78. }
  79. utftext += enc;
  80. start = end = n + 1;
  81. }
  82. }
  83.  
  84. if (end > start) {
  85. utftext += string.slice(start, stringl);
  86. }
  87.  
  88. return utftext;
  89. }
  90.  
  91.  
  92. /*
  93. CryptoJS v3.0.2
  94. code.google.com/p/crypto-js
  95. (c) 2009-2012 by Jeff Mott. All rights reserved.
  96. code.google.com/p/crypto-js/wiki/License
  97. */
  98. var CryptoJS = CryptoJS || function (h, i) {
  99. var e = {},
  100. f = e.lib = {},
  101. l = f.Base = function () {
  102. function a() {}
  103.  
  104. return {
  105. extend : function (j) {
  106. a.prototype = this;
  107. var d = new a;
  108. j && d.mixIn(j);
  109. d.$super = this;
  110. return d
  111. },
  112. create : function () {
  113. var a = this.extend();
  114. a.init.apply(a, arguments);
  115. return a
  116. },
  117. init : function () {},
  118. mixIn : function (a) {
  119. for (var d in a)
  120. a.hasOwnProperty(d) && (this[d] = a[d]);
  121. a.hasOwnProperty("toString") && (this.toString = a.toString)
  122. },
  123. clone : function () {
  124. return this.$super.extend(this)
  125. }
  126. }
  127. }
  128. (),
  129. k = f.WordArray = l.extend({
  130. init : function (a, j) {
  131. a =
  132. this.words = a || [];
  133. this.sigBytes = j != i ? j : 4 * a.length
  134. },
  135. toString : function (a) {
  136. return (a || m).stringify(this)
  137. },
  138. concat : function (a) {
  139. var j = this.words,
  140. d = a.words,
  141. c = this.sigBytes,
  142. a = a.sigBytes;
  143. this.clamp();
  144. if (c % 4)
  145. for (var b = 0; b < a; b++)
  146. j[c + b >>> 2] |= (d[b >>> 2] >>> 24 - 8 * (b % 4) & 255) << 24 - 8 * ((c + b) % 4);
  147. else if (65535 < d.length)
  148. for (b = 0; b < a; b += 4)
  149. j[c + b >>> 2] = d[b >>> 2];
  150. else
  151. j.push.apply(j, d);
  152. this.sigBytes += a;
  153. return this
  154. },
  155. clamp : function () {
  156. var a = this.words,
  157. b = this.sigBytes;
  158. a[b >>> 2] &= 4294967295 << 32 - 8 * (b % 4);
  159. a.length = h.ceil(b / 4)
  160. },
  161. clone : function () {
  162. var a =
  163. l.clone.call(this);
  164. a.words = this.words.slice(0);
  165. return a
  166. },
  167. random : function (a) {
  168. for (var b = [], d = 0; d < a; d += 4)
  169. b.push(4294967296 * h.random() | 0);
  170. return k.create(b, a)
  171. }
  172. }),
  173. o = e.enc = {},
  174. m = o.Hex = {
  175. stringify : function (a) {
  176. for (var b = a.words, a = a.sigBytes, d = [], c = 0; c < a; c++) {
  177. var e = b[c >>> 2] >>> 24 - 8 * (c % 4) & 255;
  178. d.push((e >>> 4).toString(16));
  179. d.push((e & 15).toString(16))
  180. }
  181. return d.join("")
  182. },
  183. parse : function (a) {
  184. for (var b = a.length, d = [], c = 0; c < b; c += 2)
  185. d[c >>> 3] |= parseInt(a.substr(c, 2), 16) << 24 - 4 * (c % 8);
  186. return k.create(d, b / 2)
  187. }
  188. },
  189. q = o.Latin1 = {
  190. stringify : function (a) {
  191. for (var b =
  192. a.words, a = a.sigBytes, d = [], c = 0; c < a; c++)
  193. d.push(String.fromCharCode(b[c >>> 2] >>> 24 - 8 * (c % 4) & 255));
  194. return d.join("")
  195. },
  196. parse : function (a) {
  197. for (var b = a.length, d = [], c = 0; c < b; c++)
  198. d[c >>> 2] |= (a.charCodeAt(c) & 255) << 24 - 8 * (c % 4);
  199. return k.create(d, b)
  200. }
  201. },
  202. r = o.Utf8 = {
  203. stringify : function (a) {
  204. try {
  205. return decodeURIComponent(escape(q.stringify(a)))
  206. } catch (b) {
  207. throw Error("Malformed UTF-8 data");
  208. }
  209. },
  210. parse : function (a) {
  211. return q.parse(unescape(encodeURIComponent(a)))
  212. }
  213. },
  214. b = f.BufferedBlockAlgorithm = l.extend({
  215. reset : function () {
  216. this._data = k.create();
  217. this._nDataBytes = 0
  218. },
  219. _append : function (a) {
  220. "string" == typeof a && (a = r.parse(a));
  221. this._data.concat(a);
  222. this._nDataBytes += a.sigBytes
  223. },
  224. _process : function (a) {
  225. var b = this._data,
  226. d = b.words,
  227. c = b.sigBytes,
  228. e = this.blockSize,
  229. g = c / (4 * e),
  230. g = a ? h.ceil(g) : h.max((g | 0) - this._minBufferSize, 0),
  231. a = g * e,
  232. c = h.min(4 * a, c);
  233. if (a) {
  234. for (var f = 0; f < a; f += e)
  235. this._doProcessBlock(d, f);
  236. f = d.splice(0, a);
  237. b.sigBytes -= c
  238. }
  239. return k.create(f, c)
  240. },
  241. clone : function () {
  242. var a = l.clone.call(this);
  243. a._data = this._data.clone();
  244. return a
  245. },
  246. _minBufferSize : 0
  247. });
  248. f.Hasher = b.extend({
  249. init : function () {
  250. this.reset()
  251. },
  252. reset : function () {
  253. b.reset.call(this);
  254. this._doReset()
  255. },
  256. update : function (a) {
  257. this._append(a);
  258. this._process();
  259. return this
  260. },
  261. finalize : function (a) {
  262. a && this._append(a);
  263. this._doFinalize();
  264. return this._hash
  265. },
  266. clone : function () {
  267. var a = b.clone.call(this);
  268. a._hash = this._hash.clone();
  269. return a
  270. },
  271. blockSize : 16,
  272. _createHelper : function (a) {
  273. return function (b, d) {
  274. return a.create(d).finalize(b)
  275. }
  276. },
  277. _createHmacHelper : function (a) {
  278. return function (b, d) {
  279. return g.HMAC.create(a, d).finalize(b)
  280. }
  281. }
  282. });
  283. var g = e.algo = {};
  284. return e
  285. }
  286. (Math);
  287. (function (h) {
  288. var i = CryptoJS,
  289. e = i.lib,
  290. f = e.WordArray,
  291. e = e.Hasher,
  292. l = i.algo,
  293. k = [],
  294. o = [];
  295. (function () {
  296. function e(a) {
  297. for (var b = h.sqrt(a), d = 2; d <= b; d++)
  298. if (!(a % d))
  299. return !1;
  300. return !0
  301. }
  302. function f(a) {
  303. return 4294967296 * (a - (a | 0)) | 0
  304. }
  305. for (var b = 2, g = 0; 64 > g; )
  306. e(b) && (8 > g && (k[g] = f(h.pow(b, 0.5))), o[g] = f(h.pow(b, 1 / 3)), g++), b++
  307. })();
  308. var m = [],
  309. l = l.SHA256 = e.extend({
  310. _doReset : function () {
  311. this._hash = f.create(k.slice(0))
  312. },
  313. _doProcessBlock : function (e, f) {
  314. for (var b = this._hash.words, g = b[0], a = b[1], j = b[2], d = b[3], c = b[4], h = b[5], l = b[6], k = b[7], n = 0; 64 >
  315. n; n++) {
  316. if (16 > n)
  317. m[n] = e[f + n] | 0;
  318. else {
  319. var i = m[n - 15],
  320. p = m[n - 2];
  321. m[n] = ((i << 25 | i >>> 7)^(i << 14 | i >>> 18)^i >>> 3) + m[n - 7] + ((p << 15 | p >>> 17)^(p << 13 | p >>> 19)^p >>> 10) + m[n - 16]
  322. }
  323. i = k + ((c << 26 | c >>> 6)^(c << 21 | c >>> 11)^(c << 7 | c >>> 25)) + (c & h^~c & l) + o[n] + m[n];
  324. p = ((g << 30 | g >>> 2)^(g << 19 | g >>> 13)^(g << 10 | g >>> 22)) + (g & a^g & j^a & j);
  325. k = l;
  326. l = h;
  327. h = c;
  328. c = d + i | 0;
  329. d = j;
  330. j = a;
  331. a = g;
  332. g = i + p | 0
  333. }
  334. b[0] = b[0] + g | 0;
  335. b[1] = b[1] + a | 0;
  336. b[2] = b[2] + j | 0;
  337. b[3] = b[3] + d | 0;
  338. b[4] = b[4] + c | 0;
  339. b[5] = b[5] + h | 0;
  340. b[6] = b[6] + l | 0;
  341. b[7] = b[7] + k | 0
  342. },
  343. _doFinalize : function () {
  344. var e = this._data,
  345. f = e.words,
  346. b = 8 * this._nDataBytes,
  347. g = 8 * e.sigBytes;
  348. f[g >>> 5] |= 128 << 24 - g % 32;
  349. f[(g + 64 >>> 9 << 4) + 15] = b;
  350. e.sigBytes = 4 * f.length;
  351. this._process()
  352. }
  353. });
  354. i.SHA256 = e._createHelper(l);
  355. i.HmacSHA256 = e._createHmacHelper(l)
  356. })(Math);
  357. (function () {
  358. var h = CryptoJS,
  359. i = h.enc.Utf8;
  360. h.algo.HMAC = h.lib.Base.extend({
  361. init : function (e, f) {
  362. e = this._hasher = e.create();
  363. "string" == typeof f && (f = i.parse(f));
  364. var h = e.blockSize,
  365. k = 4 * h;
  366. f.sigBytes > k && (f = e.finalize(f));
  367. for (var o = this._oKey = f.clone(), m = this._iKey = f.clone(), q = o.words, r = m.words, b = 0; b < h; b++)
  368. q[b] ^= 1549556828, r[b] ^= 909522486;
  369. o.sigBytes = m.sigBytes = k;
  370. this.reset()
  371. },
  372. reset : function () {
  373. var e = this._hasher;
  374. e.reset();
  375. e.update(this._iKey)
  376. },
  377. update : function (e) {
  378. this._hasher.update(e);
  379. return this
  380. },
  381. finalize : function (e) {
  382. var f =
  383. this._hasher,
  384. e = f.finalize(e);
  385. f.reset();
  386. return f.finalize(this._oKey.clone().concat(e))
  387. }
  388. })
  389. })();
  390.  
  391. /*
  392. CryptoJS v3.0.2
  393. code.google.com/p/crypto-js
  394. (c) 2009-2012 by Jeff Mott. All rights reserved.
  395. code.google.com/p/crypto-js/wiki/License
  396. */
  397. (function () {
  398. // Shortcuts
  399. var C = CryptoJS;
  400. var C_lib = C.lib;
  401. var WordArray = C_lib.WordArray;
  402. var C_enc = C.enc;
  403.  
  404. /**
  405. * Base64 encoding strategy.
  406. */
  407. var Base64 = C_enc.Base64 = {
  408. /**
  409. * Converts a word array to a Base64 string.
  410. *
  411. * @param {WordArray} wordArray The word array.
  412. *
  413. * @return {string} The Base64 string.
  414. *
  415. * @static
  416. *
  417. * @example
  418. *
  419. * var base64String = CryptoJS.enc.Base64.stringify(wordArray);
  420. */
  421. stringify: function (wordArray) {
  422. // Shortcuts
  423. var words = wordArray.words;
  424. var sigBytes = wordArray.sigBytes;
  425. var map = this._map;
  426.  
  427. // Clamp excess bits
  428. wordArray.clamp();
  429.  
  430. // Convert
  431. var base64Chars = [];
  432. for (var i = 0; i < sigBytes; i += 3) {
  433. var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  434. var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
  435. var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
  436.  
  437. var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
  438.  
  439. for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
  440. base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
  441. }
  442. }
  443.  
  444. // Add padding
  445. var paddingChar = map.charAt(64);
  446. if (paddingChar) {
  447. while (base64Chars.length % 4) {
  448. base64Chars.push(paddingChar);
  449. }
  450. }
  451.  
  452. return base64Chars.join('');
  453. },
  454.  
  455. /**
  456. * Converts a Base64 string to a word array.
  457. *
  458. * @param {string} base64Str The Base64 string.
  459. *
  460. * @return {WordArray} The word array.
  461. *
  462. * @static
  463. *
  464. * @example
  465. *
  466. * var wordArray = CryptoJS.enc.Base64.parse(base64String);
  467. */
  468. parse: function (base64Str) {
  469. // Ignore whitespaces
  470. base64Str = base64Str.replace(/\s/g, '');
  471.  
  472. // Shortcuts
  473. var base64StrLength = base64Str.length;
  474. var map = this._map;
  475.  
  476. // Ignore padding
  477. var paddingChar = map.charAt(64);
  478. if (paddingChar) {
  479. var paddingIndex = base64Str.indexOf(paddingChar);
  480. if (paddingIndex != -1) {
  481. base64StrLength = paddingIndex;
  482. }
  483. }
  484.  
  485. // Convert
  486. var words = [];
  487. var nBytes = 0;
  488. for (var i = 0; i < base64StrLength; i++) {
  489. if (i % 4) {
  490. var bitsHigh = map.indexOf(base64Str.charAt(i - 1)) << ((i % 4) * 2);
  491. var bitsLow = map.indexOf(base64Str.charAt(i)) >>> (6 - (i % 4) * 2);
  492. words[nBytes >>> 2] |= (bitsHigh | bitsLow) << (24 - (nBytes % 4) * 8);
  493. nBytes++;
  494. }
  495. }
  496.  
  497. return WordArray.create(words, nBytes);
  498. },
  499.  
  500. _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  501. };
  502. }());
Add Comment
Please, Sign In to add comment