keeweb/app/scripts/mixins/protected-value-ex.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const kdbxweb = require('kdbxweb');
2016-01-16 15:19:33 +01:00
2016-08-14 20:53:55 +02:00
const ExpectedFieldRefChars = '{REF:0@I:00000000000000000000000000000000}'.split('');
const ExpectedFieldRefByteLength = ExpectedFieldRefChars.length;
2016-01-16 15:19:33 +01:00
kdbxweb.ProtectedValue.prototype.isProtected = true;
2016-01-17 12:36:52 +01:00
kdbxweb.ProtectedValue.prototype.forEachChar = function(fn) {
2017-01-31 07:50:28 +01:00
const value = this._value;
const salt = this._salt;
2019-08-16 23:05:39 +02:00
let b, b1, b2, b3;
2017-01-31 07:50:28 +01:00
for (let i = 0, len = value.length; i < len; i++) {
2016-01-17 12:36:52 +01:00
b = value[i] ^ salt[i];
if (b < 128) {
2016-08-14 20:53:55 +02:00
if (fn(b) === false) {
return;
}
2016-01-17 12:36:52 +01:00
continue;
}
2019-08-16 23:05:39 +02:00
i++;
b1 = value[i] ^ salt[i];
if (i === len) {
break;
}
2016-01-17 12:36:52 +01:00
if (b >= 192 && b < 224) {
2016-08-14 20:53:55 +02:00
if (fn(((b & 0x1f) << 6) | (b1 & 0x3f)) === false) {
return;
}
2016-01-17 12:36:52 +01:00
continue;
}
2019-08-16 23:05:39 +02:00
i++;
b2 = value[i] ^ salt[i];
if (i === len) {
break;
}
2016-01-17 12:36:52 +01:00
if (b >= 224 && b < 240) {
2016-08-14 20:53:55 +02:00
if (fn(((b & 0xf) << 12) | ((b1 & 0x3f) << 6) | (b2 & 0x3f)) === false) {
return;
}
2016-01-17 12:36:52 +01:00
}
2019-08-16 23:05:39 +02:00
i++;
b3 = value[i] ^ salt[i];
if (i === len) {
break;
}
2016-01-17 12:36:52 +01:00
if (b >= 240 && b < 248) {
2017-01-31 07:50:28 +01:00
let c = ((b & 7) << 18) | ((b1 & 0x3f) << 12) | ((b2 & 0x3f) << 6) | (b3 & 0x3f);
2016-01-17 12:36:52 +01:00
if (c <= 0xffff) {
2016-08-14 20:53:55 +02:00
if (fn(c) === false) {
return;
}
2016-01-16 15:19:33 +01:00
} else {
2016-01-17 12:36:52 +01:00
c ^= 0x10000;
2016-08-14 20:53:55 +02:00
if (fn(0xd800 | (c >> 10)) === false) {
return;
}
if (fn(0xdc00 | (c & 0x3ff)) === false) {
return;
}
2016-01-16 15:19:33 +01:00
}
}
2016-01-17 12:36:52 +01:00
// skip error
}
};
Object.defineProperty(kdbxweb.ProtectedValue.prototype, 'textLength', {
get: function() {
2017-01-31 07:50:28 +01:00
let textLength = 0;
2019-08-16 23:05:39 +02:00
this.forEachChar(() => {
textLength++;
});
2016-01-16 15:19:33 +01:00
return textLength;
}
});
2016-01-17 12:36:52 +01:00
kdbxweb.ProtectedValue.prototype.includesLower = function(findLower) {
2017-01-31 07:50:28 +01:00
let matches = false;
const foundSeqs = [];
const len = findLower.length;
2016-07-17 13:30:38 +02:00
this.forEachChar(ch => {
2016-01-17 12:36:52 +01:00
ch = String.fromCharCode(ch).toLowerCase();
if (matches) {
return;
}
2017-01-31 07:50:28 +01:00
for (let i = 0; i < foundSeqs.length; i++) {
const seqIx = ++foundSeqs[i];
2016-01-17 12:36:52 +01:00
if (findLower[seqIx] !== ch) {
foundSeqs.splice(i, 1);
i--;
2016-01-17 13:44:37 +01:00
continue;
}
if (seqIx === len - 1) {
matches = true;
return;
2016-01-17 12:36:52 +01:00
}
}
if (findLower[0] === ch) {
foundSeqs.push(0);
}
});
return matches;
};
2016-01-17 14:12:35 +01:00
kdbxweb.ProtectedValue.prototype.equals = function(other) {
if (!other) {
return false;
}
if (!other.isProtected) {
return this.textLength === other.length && this.includes(other);
}
if (other === this) {
return true;
}
2017-01-31 07:50:28 +01:00
const len = this.byteLength;
2016-01-17 14:12:35 +01:00
if (len !== other.byteLength) {
return false;
}
2017-01-31 07:50:28 +01:00
for (let i = 0; i < len; i++) {
2016-01-17 14:12:35 +01:00
if ((this._value[i] ^ this._salt[i]) !== (other._value[i] ^ other._salt[i])) {
return false;
}
}
return true;
};
2016-08-14 20:53:55 +02:00
kdbxweb.ProtectedValue.prototype.isFieldReference = function() {
if (this.byteLength !== ExpectedFieldRefByteLength) {
return false;
}
let ix = 0;
this.forEachChar(ch => {
2017-01-31 07:50:28 +01:00
const expected = ExpectedFieldRefChars[ix++];
2016-08-14 20:53:55 +02:00
if (expected !== '0' && ch !== expected) {
return false;
}
});
return true;
};
2016-01-16 15:19:33 +01:00
module.exports = kdbxweb.ProtectedValue;