Skip to content

Commit

Permalink
exposed some ProtectedValue methods
Browse files Browse the repository at this point in the history
  • Loading branch information
antelle committed May 10, 2021
1 parent 1d2237a commit ae40a0f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
39 changes: 26 additions & 13 deletions lib/crypto/protected-value.ts
@@ -1,23 +1,24 @@
import * as CryptoEngine from './crypto-engine';
import {
arrayToBuffer,
base64ToBytes,
bytesToBase64,
bytesToString,
stringToBytes,
zeroBuffer
} from '../utils/byte-utils';

export class ProtectedValue {
private readonly _value: Uint8Array;
private readonly _salt: Uint8Array;
readonly value: Uint8Array;
readonly salt: Uint8Array;

constructor(value: ArrayBuffer, salt: ArrayBuffer) {
this._value = new Uint8Array(value);
this._salt = new Uint8Array(salt);
this.value = new Uint8Array(value);
this.salt = new Uint8Array(salt);
}

toString(): string {
return bytesToBase64(this._value);
return bytesToBase64(this.value);
}

static fromString(str: string): ProtectedValue {
Expand All @@ -29,6 +30,18 @@ export class ProtectedValue {
return new ProtectedValue(arrayToBuffer(bytes), arrayToBuffer(salt));
}

toBase64(): string {
const binary = this.getBinary();
const base64 = bytesToBase64(binary);
zeroBuffer(binary);
return base64;
}

static fromBase64(base64: string): ProtectedValue {
const bytes = base64ToBytes(base64);
return ProtectedValue.fromBinary(bytes);
}

/**
* Keep in mind that you're passing the ownership of this array, the contents will be destroyed
*/
Expand All @@ -45,8 +58,8 @@ export class ProtectedValue {
if (str.length === 0) {
return false;
}
const source = this._value,
salt = this._salt,
const source = this.value,
salt = this.salt,
search = stringToBytes(str),
sourceLen = source.length,
searchLen = search.length,
Expand Down Expand Up @@ -78,8 +91,8 @@ export class ProtectedValue {
}

getBinary(): Uint8Array {
const value = this._value,
salt = this._salt;
const value = this.value,
salt = this.salt;
const bytes = new Uint8Array(value.byteLength);
for (let i = bytes.length - 1; i >= 0; i--) {
bytes[i] = value[i] ^ salt[i];
Expand All @@ -89,19 +102,19 @@ export class ProtectedValue {

setSalt(newSalt: ArrayBuffer): void {
const newSaltArr = new Uint8Array(newSalt);
const value = this._value,
salt = this._salt;
const value = this.value,
salt = this.salt;
for (let i = 0, len = value.length; i < len; i++) {
value[i] = value[i] ^ salt[i] ^ newSaltArr[i];
salt[i] = newSaltArr[i];
}
}

clone(): ProtectedValue {
return new ProtectedValue(this._value, this._salt);
return new ProtectedValue(this.value, this.salt);
}

get byteLength(): number {
return this._value.byteLength;
return this.value.byteLength;
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "kdbxweb",
"version": "2.0.1",
"version": "2.0.2",
"description": "Kdbx KeePass database reader for web",
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions release-notes.md
@@ -1,5 +1,10 @@
Release notes
-------------
##### v2.0.1 (2021-05-10)
`+` exposed `ProtectedValue.salt` and `ProtectedValue.value`
`+` new static method: `ProtectedValue.fromBase64`
`+` new instance method: `ProtectedValue::toBase64`

##### v2.0.1 (2021-05-08)
`+` added `passwordHash` and `keyFileHash` to credentials

Expand Down
10 changes: 10 additions & 0 deletions test/crypto/protected-value.spec.ts
Expand Up @@ -70,4 +70,14 @@ describe('ProtectedValue', () => {
const value = ProtectedValue.fromString('test').clone();
expect(value.getText()).to.be('test');
});

it('creates a value from base64', () => {
const value = ProtectedValue.fromBase64('aGVsbG8=');
expect(value.getText()).to.be('hello');
});

it('returns base64 of the value', () => {
const value = ProtectedValue.fromString('hello');
expect(value.toBase64()).to.be('aGVsbG8=');
});
});
6 changes: 2 additions & 4 deletions test/utils/xml-utils.spec.ts
Expand Up @@ -728,10 +728,8 @@ describe('XmlUtils', () => {
}

XmlUtils.updateProtectedValuesSalt(xml.documentElement, new TestPSG());
// @ts-ignore
expect(new Uint8Array(inner!.protectedValue._salt)).to.be.eql([1, 1, 1]);
// @ts-ignore
expect(new Uint8Array(item2!.protectedValue._salt)).to.be.eql([2, 2, 2]);
expect(new Uint8Array(inner!.protectedValue.salt)).to.be.eql([1, 1, 1]);
expect(new Uint8Array(item2!.protectedValue.salt)).to.be.eql([2, 2, 2]);
});
});

Expand Down

0 comments on commit ae40a0f

Please sign in to comment.