Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | class StringXor {
|
---|
9 | constructor() {
|
---|
10 | this._value = undefined;
|
---|
11 | this._buffer = undefined;
|
---|
12 | }
|
---|
13 |
|
---|
14 | add(str) {
|
---|
15 | let buf = this._buffer;
|
---|
16 | let value;
|
---|
17 | if (buf === undefined) {
|
---|
18 | buf = this._buffer = Buffer.from(str, "latin1");
|
---|
19 | this._value = Buffer.from(buf);
|
---|
20 | return;
|
---|
21 | } else if (buf.length !== str.length) {
|
---|
22 | value = this._value;
|
---|
23 | buf = this._buffer = Buffer.from(str, "latin1");
|
---|
24 | if (value.length < buf.length) {
|
---|
25 | this._value = Buffer.allocUnsafe(buf.length);
|
---|
26 | value.copy(this._value);
|
---|
27 | this._value.fill(0, value.length);
|
---|
28 | value = this._value;
|
---|
29 | }
|
---|
30 | } else {
|
---|
31 | value = this._value;
|
---|
32 | buf.write(str, "latin1");
|
---|
33 | }
|
---|
34 | const len = buf.length;
|
---|
35 | for (let i = 0; i < len; i++) {
|
---|
36 | value[i] = value[i] ^ buf[i];
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | toString() {
|
---|
41 | return this._value === undefined ? "" : this._value.toString("latin1");
|
---|
42 | }
|
---|
43 |
|
---|
44 | updateHash(hash) {
|
---|
45 | if (this._value !== undefined) hash.update(this._value);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | module.exports = StringXor;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.