• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

javascript-obfuscator / javascript-obfuscator / 19907815758

03 Dec 2025 08:27PM UTC coverage: 97.319%. Remained the same
19907815758

push

github

sanex3339
Adjust precommit hook

1770 of 1891 branches covered (93.6%)

Branch coverage included in aggregate %.

5671 of 5755 relevant lines covered (98.54%)

34102965.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/src/utils/CryptUtils.ts
1
import { inject, injectable } from 'inversify';
6✔
2
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
6✔
3

4
import { ICryptUtils } from '../interfaces/utils/ICryptUtils';
5
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
6

7
import { base64alphabet } from '../constants/Base64Alphabet';
6✔
8

9
import { RandomGenerator } from './RandomGenerator';
6✔
10
import { Utils } from './Utils';
6✔
11

12
@injectable()
13
export class CryptUtils implements ICryptUtils {
6✔
14
    /**
15
     * @type {string}
16
     */
17
    protected readonly base64Alphabet: string = base64alphabet;
455,742✔
18

19
    /**
20
     * @type {IRandomGenerator}
21
     */
22
    private readonly randomGenerator: IRandomGenerator;
23

24
    /**
25
     * @param {IRandomGenerator} randomGenerator
26
     */
27
    public constructor(@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator) {
28
        this.randomGenerator = randomGenerator;
455,742✔
29
    }
30

31
    /**
32
     * @param {string} string
33
     * @returns {string}
34
     */
35
    public btoa(string: string): string {
36
        const chars: string = this.base64Alphabet;
5,266,125✔
37

38
        let output: string = '';
5,266,125✔
39

40
        string = encodeURIComponent(string).replace(/%([0-9A-F]{2})/g, (match: string, p1: string) => {
5,266,125✔
41
            return String.fromCharCode(parseInt(`${Utils.hexadecimalPrefix}${p1}`, 16));
10,378,098✔
42
        });
43

44
        for (
5,266,125✔
45
            let block: number | undefined, charCode: number, idx: number = 0, map: string = chars;
5,266,125✔
46
            string.charAt(idx | 0) || ((map = '='), idx % 1);
44,422,320✔
47
            output += map.charAt(63 & (block >> (8 - (idx % 1) * 8)))
48
        ) {
49
            charCode = string.charCodeAt((idx += 3 / 4));
30,944,124✔
50

51
            if (charCode > 0xff) {
30,944,124!
52
                throw new Error(
×
53
                    '\'btoa\' failed: The string to be encoded contains characters outside of the Latin1 range.'
54
                );
55
            }
56

57
            block = ((<number>block) << 8) | charCode;
30,944,124✔
58
        }
59

60
        return output;
5,266,125✔
61
    }
62

63
    /**
64
     * Hides string inside a other random string with larger length
65
     *
66
     * @param {string} str
67
     * @param {number} length
68
     * @returns {[string , string]}
69
     */
70
    public hideString(str: string, length: number): [string, string] {
71
        const escapeRegExp: (s: string) => string = (s: string) => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
894✔
72

73
        const randomMerge: (s1: string, s2: string) => string = (s1: string, s2: string): string => {
894✔
74
            let i1: number = -1;
894✔
75
            let i2: number = -1;
894✔
76
            let result: string = '';
894✔
77

78
            while (i1 < s1.length || i2 < s2.length) {
894✔
79
                if (this.randomGenerator.getMathRandom() < 0.5 && i2 < s2.length) {
58,199✔
80
                    result += s2.charAt(++i2);
28,934✔
81
                } else {
82
                    result += s1.charAt(++i1);
29,265✔
83
                }
84
            }
85

86
            return result;
894✔
87
        };
88

89
        const randomString: string = this.randomGenerator.getRandomGenerator().string({
894✔
90
            length: length,
91
            pool: RandomGenerator.randomGeneratorPool
92
        });
93

94
        let randomStringDiff: string = randomString.replace(new RegExp(`[${escapeRegExp(str)}]`, 'g'), '');
894✔
95

96
        const randomStringDiffArray: string[] = randomStringDiff.split('');
894✔
97

98
        this.randomGenerator.getRandomGenerator().shuffle(randomStringDiffArray);
894✔
99
        randomStringDiff = randomStringDiffArray.join('');
894✔
100

101
        return [randomMerge(str, randomStringDiff), randomStringDiff];
894✔
102
    }
103

104
    /**
105
     * RC4 symmetric cipher encryption/decryption
106
     * https://gist.github.com/farhadi/2185197
107
     *
108
     * @param {string} string
109
     * @param {string} key
110
     * @returns {string}
111
     */
112
    public rc4(string: string, key: string): string {
113
        const s: number[] = [];
2,650,634✔
114

115
        let j: number = 0;
2,650,634✔
116
        let x: number;
117
        let result: string = '';
2,650,634✔
118

119
        // eslint-disable-next-line no-var
120
        for (var i = 0; i < 256; i++) {
2,650,634✔
121
            s[i] = i;
678,562,304✔
122
        }
123

124
        for (i = 0; i < 256; i++) {
2,650,634✔
125
            j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
678,562,304✔
126
            x = s[i];
678,562,304✔
127
            s[i] = s[j];
678,562,304✔
128
            s[j] = x;
678,562,304✔
129
        }
130

131
        i = 0;
2,650,634✔
132
        j = 0;
2,650,634✔
133

134
        for (let y = 0; y < string.length; y++) {
2,650,634✔
135
            i = (i + 1) % 256;
8,160,500✔
136
            j = (j + s[i]) % 256;
8,160,500✔
137
            x = s[i];
8,160,500✔
138
            s[i] = s[j];
8,160,500✔
139
            s[j] = x;
8,160,500✔
140
            result += String.fromCharCode(string.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
8,160,500✔
141
        }
142

143
        return result;
2,650,634✔
144
    }
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc