webpack/lib/util/StringXor.js

56 lines
1.1 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
class StringXor {
constructor() {
this._value = undefined;
}
2021-10-04 16:36:24 +08:00
/**
* @param {string} str string
* @returns {void}
*/
add(str) {
2021-10-04 16:36:24 +08:00
const len = str.length;
const value = this._value;
if (value === undefined) {
const newValue = (this._value = Buffer.allocUnsafe(len));
for (let i = 0; i < len; i++) {
newValue[i] = str.charCodeAt(i);
}
return;
2021-10-04 16:36:24 +08:00
}
const valueLen = value.length;
if (valueLen < len) {
const newValue = (this._value = Buffer.allocUnsafe(len));
let i;
for (i = 0; i < valueLen; i++) {
newValue[i] = value[i] ^ str.charCodeAt(i);
}
for (; i < len; i++) {
newValue[i] = str.charCodeAt(i);
2020-08-14 22:19:12 +08:00
}
} else {
2021-10-04 16:36:24 +08:00
for (let i = 0; i < len; i++) {
value[i] = value[i] ^ str.charCodeAt(i);
}
}
}
toString() {
2021-10-04 16:36:24 +08:00
const value = this._value;
return value === undefined ? "" : value.toString("latin1");
}
updateHash(hash) {
2021-10-04 16:36:24 +08:00
const value = this._value;
if (value !== undefined) hash.update(value);
}
}
module.exports = StringXor;