CryptUtils.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { RandomGeneratorUtils } from './RandomGeneratorUtils';
  2. import { Utils } from './Utils';
  3. export class CryptUtils {
  4. /**
  5. * @param string
  6. */
  7. /* tslint:disable */
  8. public static btoa (string: string): string {
  9. const chars: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  10. let output: string = '';
  11. string = encodeURIComponent(string).replace(/%([0-9A-F]{2})/g, (match, p1) => {
  12. return String.fromCharCode(parseInt(`${Utils.hexadecimalPrefix}${p1}`));
  13. });
  14. for (
  15. let block: number|undefined, charCode: number, idx: number = 0, map: string = chars;
  16. string.charAt(idx | 0) || (map = '=', idx % 1);
  17. output += map.charAt(63 & block >> 8 - idx % 1 * 8)
  18. ) {
  19. charCode = string.charCodeAt(idx += 3/4);
  20. if (charCode > 0xFF) {
  21. throw new Error("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
  22. }
  23. block = block << 8 | charCode;
  24. }
  25. return output;
  26. }
  27. /* tslint:enable */
  28. /**
  29. * @param str
  30. * @param length
  31. * @returns {string[]}
  32. */
  33. public static hideString (str: string, length: number): [string, string] {
  34. const escapeRegExp: (s: string) => string = (s: string) =>
  35. s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  36. const randomMerge: (s1: string, s2: string) => string = function (s1: string, s2: string): string {
  37. let i1: number = -1,
  38. i2: number = -1,
  39. result: string = '';
  40. while (i1 < s1.length || i2 < s2.length) {
  41. if (RandomGeneratorUtils.getMathRandom() < 0.5 && i2 < s2.length) {
  42. result += s2.charAt(++i2);
  43. } else {
  44. result += s1.charAt(++i1);
  45. }
  46. }
  47. return result;
  48. };
  49. const randomString: string = RandomGeneratorUtils.getRandomGenerator().string({
  50. length: length,
  51. pool: RandomGeneratorUtils.randomGeneratorPool
  52. });
  53. let randomStringDiff: string = randomString.replace(
  54. new RegExp('[' + escapeRegExp(str) + ']', 'g'),
  55. '');
  56. const randomStringDiffArray: string[] = randomStringDiff.split('');
  57. RandomGeneratorUtils.getRandomGenerator().shuffle(randomStringDiffArray);
  58. randomStringDiff = randomStringDiffArray.join('');
  59. return [randomMerge(str, randomStringDiff), randomStringDiff];
  60. }
  61. /**
  62. * RC4 symmetric cipher encryption/decryption
  63. * https://gist.github.com/farhadi/2185197
  64. *
  65. * @param key
  66. * @param string
  67. * @returns {string}
  68. */
  69. /* tslint:disable */
  70. public static rc4 (string: string, key: string): string {
  71. let s: number[] = [],
  72. j: number = 0,
  73. x: number,
  74. result: string = '';
  75. for (var i = 0; i < 256; i++) {
  76. s[i] = i;
  77. }
  78. for (i = 0; i < 256; i++) {
  79. j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
  80. x = s[i];
  81. s[i] = s[j];
  82. s[j] = x;
  83. }
  84. i = 0;
  85. j = 0;
  86. for (let y = 0; y < string.length; y++) {
  87. i = (i + 1) % 256;
  88. j = (j + s[i]) % 256;
  89. x = s[i];
  90. s[i] = s[j];
  91. s[j] = x;
  92. result += String.fromCharCode(string.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
  93. }
  94. return result;
  95. }
  96. /* tslint:enable */
  97. }