Skip to content

Commit

Permalink
Binary should have method to b64
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanovSPvirtru committed Sep 6, 2023
1 parent c1936d2 commit ffec1d9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
17 changes: 17 additions & 0 deletions lib/tdf3/src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export abstract class Binary {

abstract asArrayBuffer(): ArrayBuffer;

abstract asB64(): string;

abstract asByteArray(): number[];

abstract asString(): string;
Expand Down Expand Up @@ -95,6 +97,11 @@ class ArrayBufferBinary extends Binary {
return new TextDecoder().decode(uint8Array);
}

override asB64(): string {
const uint8Array = new Uint8Array(this.value);
return window.btoa([...uint8Array].map(byte => String.fromCharCode(byte)).join(''));
}

override isArrayBuffer(): boolean {
return true;
}
Expand Down Expand Up @@ -131,6 +138,11 @@ class ByteArrayBinary extends Binary {
return new TextDecoder().decode(uint8Array);
}

override asB64(): string {
const uint8Array = new Uint8Array(this.value);
return window.btoa([...uint8Array].map(byte => String.fromCharCode(byte)).join(''));
}

override isByteArray(): boolean {
return true;
}
Expand Down Expand Up @@ -175,6 +187,11 @@ class StringBinary extends Binary {
return this.value;
}

override asB64(): string {
const uint8Array: Uint8Array = new TextEncoder().encode(this.value);
return window.btoa([...uint8Array].map(byte => String.fromCharCode(byte)).join(''));
}

override isString(): boolean {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/tdf3/src/models/encryption-information.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class SplitKey {
);

const encryptedMetadataOb = {
ciphertext: base64.encode(encryptedMetadataResult.payload.asString()),
iv: base64.encode(keyInfo.unwrappedKeyIvBinary.asString()),
ciphertext: encryptedMetadataResult.payload.asB64(),
iv: keyInfo.unwrappedKeyIvBinary.asB64(),
};

const encryptedMetadataStr = JSON.stringify(encryptedMetadataOb);
Expand Down Expand Up @@ -143,7 +143,7 @@ export class SplitKey {
method: {
algorithm,
isStreamable: false,
iv: base64.encode(keyInfo.unwrappedKeyIvBinary.asString()),
iv: keyInfo.unwrappedKeyIvBinary.asB64(),
},
integrityInformation: {
rootSignature: {
Expand Down
5 changes: 2 additions & 3 deletions lib/tdf3/src/models/key-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Wrapped {
type: 'wrapped',
url: this.url,
protocol: 'kas',
wrappedKey: base64.encode(wrappedKeyBinary.asString()),
wrappedKey: wrappedKeyBinary.asB64(),
encryptedMetadata: base64.encode(encryptedMetadataStr),
policyBinding: base64.encode(policyBinding),
};
Expand Down Expand Up @@ -77,8 +77,7 @@ export class Remote {
this.publicKey
);

// this.wrappedKey = wrappedKeyBinary.asBuffer().toString('hex');
this.wrappedKey = base64.encode(wrappedKeyBinary.asString());
this.wrappedKey = wrappedKeyBinary.asB64();

this.keyAccessObject = {
type: 'remote',
Expand Down
2 changes: 1 addition & 1 deletion lib/tdf3/src/tdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ export class TDF extends EventEmitter {
if (upsertResponse) {
plaintextStream.upsertResponse = upsertResponse;
plaintextStream.tdfSize = totalByteCount;
plaintextStream.KEK = payloadKey ? null : btoa(kek.payload.asString());
plaintextStream.KEK = payloadKey ? null : kek.payload.asB64();
plaintextStream.algorithm = manifest.encryptionInformation.method.algorithm;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tests/mocha/encrypt-decrypt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('encrypt decrypt test', async function () {

const encryptedStream = await client.encrypt({
eo,
metadata: Mocks.getMetadataObject(),
metadata: {},
offline: true,
scope,
source: new ReadableStream({
Expand Down

0 comments on commit ffec1d9

Please sign in to comment.