How to use the @tanker/types.castData function in @tanker/types

To help you get started, we’ve selected a few @tanker/types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TankerHQ / sdk-js / packages / stream / base / src / __tests__ / MergerStream.spec.js View on Github external
stream.on('end', async () => {
          try {
            expect(output).to.have.lengthOf(1);
            expect(output[0]).to.be.an.instanceOf(type);

            const outputBytes = await castData(output[0], { type: Uint8Array });
            expect(outputBytes).to.deep.equal(bytes);

            if (global.Blob && output[0] instanceof global.Blob) {
              // $FlowExpectedError
              expect(output[0].type).to.equal(options.mime);
            }
            if (global.File && output[0] instanceof global.File) {
              // $FlowExpectedError
              expect(output[0].name).to.equal(options.name);
            }
            resolve();
          } catch (e) {
            reject(e);
          }
        });
      });
github TankerHQ / sdk-js / packages / core / src / DataProtection / DataProtector.js View on Github external
const progressHandler = new ProgressHandler(progressOptions).start(clearSize);

    const resourceId = encryption.extractResourceId(castEncryptedData);
    const key = await this._resourceManager.findKeyFromResourceId(resourceId);

    let clearData;

    try {
    // $FlowIKnow Already checked we are using a simple encryption
      clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
    } catch (error) {
      const b64ResourceId = utils.toBase64(resourceId);
      throw new DecryptionFailed({ error, b64ResourceId });
    }

    const castClearData = await castData(clearData, outputOptions);

    progressHandler.report(clearSize);

    return castClearData;
  }
github TankerHQ / sdk-js / packages / stream / base / src / MergerStream.js View on Github external
async _pushLastChunk() {
    // Always push last chunk even if zero bytes
    const uint8array = this._buffer.consume(this._buffer.byteSize());
    const lastChunk = await castData(uint8array, this._options);
    this.push(lastChunk);
  }
}
github TankerHQ / sdk-js / packages / core / src / DataProtection / DataProtector.js View on Github external
async _simpleDecryptData(encryptedData: Data, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
    const castEncryptedData = await castData(encryptedData, { type: Uint8Array });

    const encryption = extractEncryptionFormat(castEncryptedData);
    const encryptedSize = getDataLength(castEncryptedData);
    // $FlowIKnow Already checked we are using a simple encryption
    const clearSize = encryption.getClearSize(encryptedSize);
    const progressHandler = new ProgressHandler(progressOptions).start(clearSize);

    const resourceId = encryption.extractResourceId(castEncryptedData);
    const key = await this._resourceManager.findKeyFromResourceId(resourceId);

    let clearData;

    try {
    // $FlowIKnow Already checked we are using a simple encryption
      clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
    } catch (error) {
github TankerHQ / sdk-js / packages / core / src / DataProtection / DataProtector.js View on Github external
async _simpleEncryptData(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
    const encryption = getSimpleEncryption();

    const clearSize = getDataLength(clearData);
    const encryptedSize = encryption.getEncryptedSize(clearSize);
    const progressHandler = new ProgressHandler(progressOptions).start(encryptedSize);

    const castClearData = await castData(clearData, { type: Uint8Array });
    const { key } = makeResource();
    const encryptedData = encryption.serialize(encryption.encrypt(key, castClearData));
    const resourceId = encryption.extractResourceId(encryptedData);
    await this._shareResources([{ resourceId, key }], sharingOptions, true);
    const castEncryptedData = await castData(encryptedData, outputOptions);

    progressHandler.report(encryptedSize);

    return castEncryptedData;
  }
github TankerHQ / sdk-js / packages / core / src / DataProtection / DataProtector.js View on Github external
async _simpleEncryptData(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
    const encryption = getSimpleEncryption();

    const clearSize = getDataLength(clearData);
    const encryptedSize = encryption.getEncryptedSize(clearSize);
    const progressHandler = new ProgressHandler(progressOptions).start(encryptedSize);

    const castClearData = await castData(clearData, { type: Uint8Array });
    const { key } = makeResource();
    const encryptedData = encryption.serialize(encryption.encrypt(key, castClearData));
    const resourceId = encryption.extractResourceId(encryptedData);
    await this._shareResources([{ resourceId, key }], sharingOptions, true);
    const castEncryptedData = await castData(encryptedData, outputOptions);

    progressHandler.report(encryptedSize);

    return castEncryptedData;
  }
github TankerHQ / sdk-js / packages / core / src / DataProtection / DataProtector.js View on Github external
async decryptData(encryptedData: Data, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
    const leadingBytes = await castData(encryptedData, { type: Uint8Array }, SAFE_EXTRACTION_LENGTH);
    const encryption = extractEncryptionFormat(leadingBytes);

    if (encryption.features.chunks)
      return this._streamDecryptData(encryptedData, outputOptions, progressOptions);

    return this._simpleDecryptData(encryptedData, outputOptions, progressOptions);
  }
github TankerHQ / sdk-js / packages / core / src / Tanker.js View on Github external
async getResourceId(encryptedData: Uint8Array): Promise {
    this.assert(statuses.READY, 'get a resource id');
    assertDataType(encryptedData, 'encryptedData');

    const castEncryptedData = await castData(encryptedData, { type: Uint8Array }, SAFE_EXTRACTION_LENGTH);

    const encryption = extractEncryptionFormat(castEncryptedData);

    return utils.toBase64(encryption.extractResourceId(castEncryptedData));
  }

@tanker/types

Tanker SDK (types)

Apache-2.0
Latest version published 5 months ago

Package Health Score

65 / 100
Full package analysis

Similar packages