1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | class StringXor {
|
9 | constructor() {
|
10 | this._value = undefined;
|
11 | }
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 | add(str) {
|
18 | const len = str.length;
|
19 | const value = this._value;
|
20 | if (value === undefined) {
|
21 | const newValue = (this._value = Buffer.allocUnsafe(len));
|
22 | for (let i = 0; i < len; i++) {
|
23 | newValue[i] = str.charCodeAt(i);
|
24 | }
|
25 | return;
|
26 | }
|
27 | const valueLen = value.length;
|
28 | if (valueLen < len) {
|
29 | const newValue = (this._value = Buffer.allocUnsafe(len));
|
30 | let i;
|
31 | for (i = 0; i < valueLen; i++) {
|
32 | newValue[i] = value[i] ^ str.charCodeAt(i);
|
33 | }
|
34 | for (; i < len; i++) {
|
35 | newValue[i] = str.charCodeAt(i);
|
36 | }
|
37 | } else {
|
38 | for (let i = 0; i < len; i++) {
|
39 | value[i] = value[i] ^ str.charCodeAt(i);
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 | toString() {
|
45 | const value = this._value;
|
46 | return value === undefined ? "" : value.toString("latin1");
|
47 | }
|
48 |
|
49 | updateHash(hash) {
|
50 | const value = this._value;
|
51 | if (value !== undefined) hash.update(value);
|
52 | }
|
53 | }
|
54 |
|
55 | module.exports = StringXor;
|