base64x-1.1.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*! base64x-1.1.5 (c) 2012-2015 Kenji Urushima | kjur.github.com/jsjws/license
  2. */
  3. /*
  4. * base64x.js - Base64url and supplementary functions for Tom Wu's base64.js library
  5. *
  6. * version: 1.1.5 (2015-Sep-13)
  7. *
  8. * Copyright (c) 2012-2015 Kenji Urushima (kenji.urushima@gmail.com)
  9. *
  10. * This software is licensed under the terms of the MIT License.
  11. * http://kjur.github.com/jsjws/license/
  12. *
  13. * The above copyright and license notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * DEPENDS ON:
  17. * - base64.js - Tom Wu's Base64 library
  18. */
  19. /**
  20. * @fileOverview
  21. * @name base64x-1.1.js
  22. * @author Kenji Urushima kenji.urushima@gmail.com
  23. * @version asn1 1.1.5 (2015-Sep-13)
  24. * @since jsrsasign 2.1
  25. * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
  26. */
  27. /**
  28. * Base64URL and supplementary functions for Tom Wu's base64.js library.<br/>
  29. * This class is just provide information about global functions
  30. * defined in 'base64x.js'. The 'base64x.js' script file provides
  31. * global functions for converting following data each other.
  32. * <ul>
  33. * <li>(ASCII) String</li>
  34. * <li>UTF8 String including CJK, Latin and other characters</li>
  35. * <li>byte array</li>
  36. * <li>hexadecimal encoded String</li>
  37. * <li>Full URIComponent encoded String (such like "%69%94")</li>
  38. * <li>Base64 encoded String</li>
  39. * <li>Base64URL encoded String</li>
  40. * </ul>
  41. * All functions in 'base64x.js' are defined in {@link _global_} and not
  42. * in this class.
  43. *
  44. * @class Base64URL and supplementary functions for Tom Wu's base64.js library
  45. * @author Kenji Urushima
  46. * @version 1.1 (07 May 2012)
  47. * @requires base64.js
  48. * @see <a href="http://kjur.github.com/jsjws/">'jwjws'(JWS JavaScript Library) home page http://kjur.github.com/jsjws/</a>
  49. * @see <a href="http://kjur.github.com/jsrsasigns/">'jwrsasign'(RSA Sign JavaScript Library) home page http://kjur.github.com/jsrsasign/</a>
  50. */
  51. function Base64x() {
  52. }
  53. // ==== string / byte array ================================
  54. /**
  55. * convert a string to an array of character codes
  56. * @param {String} s
  57. * @return {Array of Numbers}
  58. */
  59. function stoBA(s) {
  60. var a = new Array();
  61. for (var i = 0; i < s.length; i++) {
  62. a[i] = s.charCodeAt(i);
  63. }
  64. return a;
  65. }
  66. /**
  67. * convert an array of character codes to a string
  68. * @param {Array of Numbers} a array of character codes
  69. * @return {String} s
  70. */
  71. function BAtos(a) {
  72. var s = "";
  73. for (var i = 0; i < a.length; i++) {
  74. s = s + String.fromCharCode(a[i]);
  75. }
  76. return s;
  77. }
  78. // ==== byte array / hex ================================
  79. /**
  80. * convert an array of bytes(Number) to hexadecimal string.<br/>
  81. * @param {Array of Numbers} a array of bytes
  82. * @return {String} hexadecimal string
  83. */
  84. function BAtohex(a) {
  85. var s = "";
  86. for (var i = 0; i < a.length; i++) {
  87. var hex1 = a[i].toString(16);
  88. if (hex1.length == 1) hex1 = "0" + hex1;
  89. s = s + hex1;
  90. }
  91. return s;
  92. }
  93. // ==== string / hex ================================
  94. /**
  95. * convert a ASCII string to a hexadecimal string of ASCII codes.<br/>
  96. * NOTE: This can't be used for non ASCII characters.
  97. * @param {s} s ASCII string
  98. * @return {String} hexadecimal string
  99. */
  100. function stohex(s) {
  101. return BAtohex(stoBA(s));
  102. }
  103. // ==== string / base64 ================================
  104. /**
  105. * convert a ASCII string to a Base64 encoded string.<br/>
  106. * NOTE: This can't be used for non ASCII characters.
  107. * @param {s} s ASCII string
  108. * @return {String} Base64 encoded string
  109. */
  110. function stob64(s) {
  111. return hex2b64(stohex(s));
  112. }
  113. // ==== string / base64url ================================
  114. /**
  115. * convert a ASCII string to a Base64URL encoded string.<br/>
  116. * NOTE: This can't be used for non ASCII characters.
  117. * @param {s} s ASCII string
  118. * @return {String} Base64URL encoded string
  119. */
  120. function stob64u(s) {
  121. return b64tob64u(hex2b64(stohex(s)));
  122. }
  123. /**
  124. * convert a Base64URL encoded string to a ASCII string.<br/>
  125. * NOTE: This can't be used for Base64URL encoded non ASCII characters.
  126. * @param {s} s Base64URL encoded string
  127. * @return {String} ASCII string
  128. */
  129. function b64utos(s) {
  130. return BAtos(b64toBA(b64utob64(s)));
  131. }
  132. // ==== base64 / base64url ================================
  133. /**
  134. * convert a Base64 encoded string to a Base64URL encoded string.<br/>
  135. * Example: "ab+c3f/==" &rarr; "ab-c3f_"
  136. * @param {String} s Base64 encoded string
  137. * @return {String} Base64URL encoded string
  138. */
  139. function b64tob64u(s) {
  140. s = s.replace(/\=/g, "");
  141. s = s.replace(/\+/g, "-");
  142. s = s.replace(/\//g, "_");
  143. return s;
  144. }
  145. /**
  146. * convert a Base64URL encoded string to a Base64 encoded string.<br/>
  147. * Example: "ab-c3f_" &rarr; "ab+c3f/=="
  148. * @param {String} s Base64URL encoded string
  149. * @return {String} Base64 encoded string
  150. */
  151. function b64utob64(s) {
  152. if (s.length % 4 == 2) s = s + "==";
  153. else if (s.length % 4 == 3) s = s + "=";
  154. s = s.replace(/-/g, "+");
  155. s = s.replace(/_/g, "/");
  156. return s;
  157. }
  158. // ==== hex / base64url ================================
  159. /**
  160. * convert a hexadecimal string to a Base64URL encoded string.<br/>
  161. * @param {String} s hexadecimal string
  162. * @return {String} Base64URL encoded string
  163. * @description
  164. * convert a hexadecimal string to a Base64URL encoded string.
  165. * NOTE: If leading "0" is omitted and odd number length for
  166. * hexadecimal leading "0" is automatically added.
  167. */
  168. function hextob64u(s) {
  169. if (s.length % 2 == 1) s = "0" + s;
  170. return b64tob64u(hex2b64(s));
  171. }
  172. /**
  173. * convert a Base64URL encoded string to a hexadecimal string.<br/>
  174. * @param {String} s Base64URL encoded string
  175. * @return {String} hexadecimal string
  176. */
  177. function b64utohex(s) {
  178. return b64tohex(b64utob64(s));
  179. }
  180. var utf8tob64u, b64utoutf8;
  181. if (typeof Buffer === 'function')
  182. {
  183. utf8tob64u = function (s)
  184. {
  185. return b64tob64u(new Buffer(s, 'utf8').toString('base64'));
  186. };
  187. b64utoutf8 = function (s)
  188. {
  189. return new Buffer(b64utob64(s), 'base64').toString('utf8');
  190. };
  191. }
  192. else
  193. {
  194. // ==== utf8 / base64url ================================
  195. /**
  196. * convert a UTF-8 encoded string including CJK or Latin to a Base64URL encoded string.<br/>
  197. * @param {String} s UTF-8 encoded string
  198. * @return {String} Base64URL encoded string
  199. * @since 1.1
  200. */
  201. utf8tob64u = function (s)
  202. {
  203. return hextob64u(uricmptohex(encodeURIComponentAll(s)));
  204. };
  205. /**
  206. * convert a Base64URL encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
  207. * @param {String} s Base64URL encoded string
  208. * @return {String} UTF-8 encoded string
  209. * @since 1.1
  210. */
  211. b64utoutf8 = function (s)
  212. {
  213. return decodeURIComponent(hextouricmp(b64utohex(s)));
  214. };
  215. }
  216. // ==== utf8 / base64url ================================
  217. /**
  218. * convert a UTF-8 encoded string including CJK or Latin to a Base64 encoded string.<br/>
  219. * @param {String} s UTF-8 encoded string
  220. * @return {String} Base64 encoded string
  221. * @since 1.1.1
  222. */
  223. function utf8tob64(s) {
  224. return hex2b64(uricmptohex(encodeURIComponentAll(s)));
  225. }
  226. /**
  227. * convert a Base64 encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
  228. * @param {String} s Base64 encoded string
  229. * @return {String} UTF-8 encoded string
  230. * @since 1.1.1
  231. */
  232. function b64toutf8(s) {
  233. return decodeURIComponent(hextouricmp(b64tohex(s)));
  234. }
  235. // ==== utf8 / hex ================================
  236. /**
  237. * convert a UTF-8 encoded string including CJK or Latin to a hexadecimal encoded string.<br/>
  238. * @param {String} s UTF-8 encoded string
  239. * @return {String} hexadecimal encoded string
  240. * @since 1.1.1
  241. */
  242. function utf8tohex(s) {
  243. return uricmptohex(encodeURIComponentAll(s));
  244. }
  245. /**
  246. * convert a hexadecimal encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
  247. * Note that when input is improper hexadecimal string as UTF-8 string, this function returns
  248. * 'null'.
  249. * @param {String} s hexadecimal encoded string
  250. * @return {String} UTF-8 encoded string or null
  251. * @since 1.1.1
  252. */
  253. function hextoutf8(s) {
  254. return decodeURIComponent(hextouricmp(s));
  255. }
  256. /**
  257. * convert a hexadecimal encoded string to raw string including non printable characters.<br/>
  258. * @param {String} s hexadecimal encoded string
  259. * @return {String} raw string
  260. * @since 1.1.2
  261. * @example
  262. * hextorstr("610061") &rarr; "a\x00a"
  263. */
  264. function hextorstr(sHex) {
  265. var s = "";
  266. for (var i = 0; i < sHex.length - 1; i += 2) {
  267. s += String.fromCharCode(parseInt(sHex.substr(i, 2), 16));
  268. }
  269. return s;
  270. }
  271. /**
  272. * convert a raw string including non printable characters to hexadecimal encoded string.<br/>
  273. * @param {String} s raw string
  274. * @return {String} hexadecimal encoded string
  275. * @since 1.1.2
  276. * @example
  277. * rstrtohex("a\x00a") &rarr; "610061"
  278. */
  279. function rstrtohex(s) {
  280. var result = "";
  281. for (var i = 0; i < s.length; i++) {
  282. result += ("0" + s.charCodeAt(i).toString(16)).slice(-2);
  283. }
  284. return result;
  285. }
  286. // ==== hex / b64nl =======================================
  287. /*
  288. * since base64x 1.1.3
  289. */
  290. function hextob64(s) {
  291. return hex2b64(s);
  292. }
  293. /*
  294. * since base64x 1.1.3
  295. */
  296. function hextob64nl(s) {
  297. var b64 = hextob64(s);
  298. var b64nl = b64.replace(/(.{64})/g, "$1\r\n");
  299. b64nl = b64nl.replace(/\r\n$/, '');
  300. return b64nl;
  301. }
  302. /*
  303. * since base64x 1.1.3
  304. */
  305. function b64nltohex(s) {
  306. var b64 = s.replace(/[^0-9A-Za-z\/+=]*/g, '');
  307. var hex = b64tohex(b64);
  308. return hex;
  309. }
  310. // ==== URIComponent / hex ================================
  311. /**
  312. * convert a URLComponent string such like "%67%68" to a hexadecimal string.<br/>
  313. * @param {String} s URIComponent string such like "%67%68"
  314. * @return {String} hexadecimal string
  315. * @since 1.1
  316. */
  317. function uricmptohex(s) {
  318. return s.replace(/%/g, "");
  319. }
  320. /**
  321. * convert a hexadecimal string to a URLComponent string such like "%67%68".<br/>
  322. * @param {String} s hexadecimal string
  323. * @return {String} URIComponent string such like "%67%68"
  324. * @since 1.1
  325. */
  326. function hextouricmp(s) {
  327. return s.replace(/(..)/g, "%$1");
  328. }
  329. // ==== URIComponent ================================
  330. /**
  331. * convert UTFa hexadecimal string to a URLComponent string such like "%67%68".<br/>
  332. * Note that these "<code>0-9A-Za-z!'()*-._~</code>" characters will not
  333. * converted to "%xx" format by builtin 'encodeURIComponent()' function.
  334. * However this 'encodeURIComponentAll()' function will convert
  335. * all of characters into "%xx" format.
  336. * @param {String} s hexadecimal string
  337. * @return {String} URIComponent string such like "%67%68"
  338. * @since 1.1
  339. */
  340. function encodeURIComponentAll(u8) {
  341. var s = encodeURIComponent(u8);
  342. var s2 = "";
  343. for (var i = 0; i < s.length; i++) {
  344. if (s[i] == "%") {
  345. s2 = s2 + s.substr(i, 3);
  346. i = i + 2;
  347. } else {
  348. s2 = s2 + "%" + stohex(s[i]);
  349. }
  350. }
  351. return s2;
  352. }
  353. // ==== new lines ================================
  354. /**
  355. * convert all DOS new line("\r\n") to UNIX new line("\n") in
  356. * a String "s".
  357. * @param {String} s string
  358. * @return {String} converted string
  359. */
  360. function newline_toUnix(s) {
  361. s = s.replace(/\r\n/mg, "\n");
  362. return s;
  363. }
  364. /**
  365. * convert all UNIX new line("\r\n") to DOS new line("\n") in
  366. * a String "s".
  367. * @param {String} s string
  368. * @return {String} converted string
  369. */
  370. function newline_toDos(s) {
  371. s = s.replace(/\r\n/mg, "\n");
  372. s = s.replace(/\n/mg, "\r\n");
  373. return s;
  374. }
  375. // ==== others ================================
  376. /**
  377. * find index of string where two string differs
  378. * @param {String} s1 string to compare
  379. * @param {String} s2 string to compare
  380. * @return {Number} string index of where character differs. Return -1 if same.
  381. * @since jsrsasign 4.9.0 base64x 1.1.5
  382. * @example
  383. * strdiffidx("abcdefg", "abcd4fg") -> 4
  384. * strdiffidx("abcdefg", "abcdefg") -> -1
  385. * strdiffidx("abcdefg", "abcdef") -> 6
  386. * strdiffidx("abcdefgh", "abcdef") -> 6
  387. */
  388. var strdiffidx = function(s1, s2) {
  389. var n = s1.length;
  390. if (s1.length > s2.length) n = s2.length;
  391. for (var i = 0; i < n; i++) {
  392. if (s1.charCodeAt(i) != s2.charCodeAt(i)) return i;
  393. }
  394. if (s1.length != s2.length) return n;
  395. return -1; // same
  396. };