-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCryptUtils.ts
More file actions
145 lines (116 loc) · 4.34 KB
/
CryptUtils.ts
File metadata and controls
145 lines (116 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { inject, injectable } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
import { ICryptUtils } from '../interfaces/utils/ICryptUtils';
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
import { base64alphabet } from '../constants/Base64Alphabet';
import { RandomGenerator } from './RandomGenerator';
import { Utils } from './Utils';
@injectable()
export class CryptUtils implements ICryptUtils {
/**
* @type {string}
*/
protected readonly base64Alphabet: string = base64alphabet;
/**
* @type {IRandomGenerator}
*/
private readonly randomGenerator: IRandomGenerator;
/**
* @param {IRandomGenerator} randomGenerator
*/
public constructor(@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator) {
this.randomGenerator = randomGenerator;
}
/**
* @param {string} string
* @returns {string}
*/
public btoa(string: string): string {
const chars: string = this.base64Alphabet;
let output: string = '';
string = encodeURIComponent(string).replace(/%([0-9A-F]{2})/g, (match: string, p1: string) => {
return String.fromCharCode(parseInt(`${Utils.hexadecimalPrefix}${p1}`, 16));
});
for (
let block: number | undefined, charCode: number, idx: number = 0, map: string = chars;
string.charAt(idx | 0) || ((map = '='), idx % 1);
output += map.charAt(63 & (block >> (8 - (idx % 1) * 8)))
) {
charCode = string.charCodeAt((idx += 3 / 4));
if (charCode > 0xff) {
throw new Error(
'\'btoa\' failed: The string to be encoded contains characters outside of the Latin1 range.'
);
}
block = ((<number>block) << 8) | charCode;
}
return output;
}
/**
* Hides string inside a other random string with larger length
*
* @param {string} str
* @param {number} length
* @returns {[string , string]}
*/
public hideString(str: string, length: number): [string, string] {
const escapeRegExp: (s: string) => string = (s: string) => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const randomMerge: (s1: string, s2: string) => string = (s1: string, s2: string): string => {
let i1: number = -1;
let i2: number = -1;
let result: string = '';
while (i1 < s1.length || i2 < s2.length) {
if (this.randomGenerator.getMathRandom() < 0.5 && i2 < s2.length) {
result += s2.charAt(++i2);
} else {
result += s1.charAt(++i1);
}
}
return result;
};
const randomString: string = this.randomGenerator.getRandomGenerator().string({
length: length,
pool: RandomGenerator.randomGeneratorPool
});
let randomStringDiff: string = randomString.replace(new RegExp(`[${escapeRegExp(str)}]`, 'g'), '');
const randomStringDiffArray: string[] = randomStringDiff.split('');
this.randomGenerator.getRandomGenerator().shuffle(randomStringDiffArray);
randomStringDiff = randomStringDiffArray.join('');
return [randomMerge(str, randomStringDiff), randomStringDiff];
}
/**
* RC4 symmetric cipher encryption/decryption
* https://gist.github.com/farhadi/2185197
*
* @param {string} string
* @param {string} key
* @returns {string}
*/
public rc4(string: string, key: string): string {
const s: number[] = [];
let j: number = 0;
let x: number;
let result: string = '';
// eslint-disable-next-line no-var
for (var i = 0; i < 256; i++) {
s[i] = i;
}
for (i = 0; i < 256; i++) {
j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
}
i = 0;
j = 0;
for (let y = 0; y < string.length; y++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
result += String.fromCharCode(string.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
}
return result;
}
}