How to use the @tanker/crypto.tcrypto.SYMMETRIC_KEY_SIZE function in @tanker/crypto

To help you get started, we’ve selected a few @tanker/crypto 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 / core / src / DataProtection / ChunkEncryptor.js View on Github external
throw new InvalidArgument('clearData', 'Uint8Array', clearData);

    if (typeof index !== 'undefined' && typeof index !== 'number')
      throw new InvalidArgument('index', 'number', index);

    if (typeof index === 'number' && index < 0)
      throw new ChunkIndexOutOfRange(index, this.chunkKeys.length);

    const idx = typeof index === 'number' ? index : this.chunkKeys.length;

    if (idx > this.chunkKeys.length) {
      const rangeSize = idx - this.chunkKeys.length;
      this.chunkKeys.push(...makeNullArray(rangeSize));
    }

    const ephemeralKey = random(tcrypto.SYMMETRIC_KEY_SIZE);
    // do not use encryptor.encrypt here, it would save the key and possibly share it!
    const encryptedData = EncryptorV1.encrypt(ephemeralKey, clearData);
    this.chunkKeys[idx] = ephemeralKey;
    return encryptedData;
  }
github TankerHQ / sdk-js / packages / core / src / __tests__ / LocalUserSerialization.spec.js View on Github external
it('should serialize/unserialize a TrustchainCreation', async () => {
    const trustchainCreation = {
      public_signature_key: random(tcrypto.SYMMETRIC_KEY_SIZE),
    };

    expect(unserializeTrustchainCreation(serializeTrustchainCreation(trustchainCreation))).to.deep.equal(trustchainCreation);
  });
});
github TankerHQ / sdk-js / packages / core / src / __tests__ / ResourceStore.spec.js View on Github external
it('ignores updates to a resource key', async () => {
    const key = random(tcrypto.SYMMETRIC_KEY_SIZE);
    const key2 = random(tcrypto.SYMMETRIC_KEY_SIZE);
    const resourceId = random(tcrypto.MAC_SIZE);

    await sharedKeystore.saveResourceKey(resourceId, key);
    await sharedKeystore.saveResourceKey(resourceId, key2);
    const thekey = await sharedKeystore.findResourceKey(resourceId);
    expect(thekey).to.deep.equal(key);
  });
});
github TankerHQ / sdk-js / packages / core / src / __tests__ / TestGenerator.js View on Github external
makeKeyPublishToUser = (parentDevice: TestDeviceCreation, recipient: User): TestKeyPublish => {
    const resourceKey = random(tcrypto.SYMMETRIC_KEY_SIZE);
    const resourceId = random(tcrypto.MAC_SIZE);
    const lastUserKey = getLastUserPublicKey(recipient);
    if (!lastUserKey) {
      throw new Error('flow check');
    }
    const { payload, nature } = makeKeyPublish(lastUserKey, resourceKey, resourceId, NATURE_KIND.key_publish_to_user);
    const { block } = createBlock(payload, nature, this._trustchainId, parentDevice.testDevice.id, parentDevice.testDevice.signKeys.privateKey);
    const keyPublish = getKeyPublishEntryFromBlock(block);

    return {
      keyPublish,
      block,
      resourceId,
      resourceKey
    };
  }
github TankerHQ / sdk-js / packages / core / src / DataProtection / __tests__ / DecryptorStream.spec.js View on Github external
beforeEach(() => {
    key = random(tcrypto.SYMMETRIC_KEY_SIZE);
    resourceId = random(16);
    mapper = { findKey: () => Promise.resolve(key) };
    stream = new DecryptorStream(mapper);
    sync = watchStream(stream);
  });
github TankerHQ / sdk-js / packages / core / src / __tests__ / TestGenerator.js View on Github external
makeKeyPublishToGroup = (parentDevice: TestDeviceCreation, recipient: Group): TestKeyPublish => {
    const resourceKey = random(tcrypto.SYMMETRIC_KEY_SIZE);
    const resourceId = random(tcrypto.MAC_SIZE);

    const { payload, nature } = makeKeyPublish(recipient.publicEncryptionKey, resourceKey, resourceId, NATURE_KIND.key_publish_to_user_group);
    const { block } = createBlock(payload, nature, this._trustchainId, parentDevice.testDevice.id, parentDevice.testDevice.signKeys.privateKey);

    const keyPublish = getKeyPublishEntryFromBlock(block);

    return {
      keyPublish,
      block,
      resourceId,
      resourceKey
    };
  }
github TankerHQ / sdk-js / packages / core / src / DataProtection / types.js View on Github external
export function makeResource(): Resource {
  const key = random(tcrypto.SYMMETRIC_KEY_SIZE);
  const resourceId = generichash(key, tcrypto.MAC_SIZE);
  return { key, resourceId };
}
github TankerHQ / sdk-js / packages / core / src / Resource / ResourceManager.js View on Github external
makeStreamResource(): ResourceMeta {
    const key = random(tcrypto.SYMMETRIC_KEY_SIZE);
    const resourceId = generichash(key, tcrypto.MAC_SIZE);

    return { key, resourceId };
  }
github TankerHQ / sdk-js / packages / core / src / Encryption / Encryptor.js View on Github external
export async function encryptData(plain: Uint8Array): Promise {
  const key = random(tcrypto.SYMMETRIC_KEY_SIZE);
  const buffer = await aead.encryptAEADv2(key, plain);
  const resourceId = aead.extractResourceId(buffer);
  const encodedVersion = varint.encode(currentVersion);
  return { key, resourceId, encryptedData: concatArrays(encodedVersion, buffer) };
}
github TankerHQ / sdk-js / packages / core / src / DataProtection / ChunkEncryptor.js View on Github external
serialize(): Uint8Array {
    const serializedEmptyRanges = serializeEmptyRanges(this.emptyRanges);
    const emptyRangesSize = new Uint8Array(varint.encode(serializedEmptyRanges.length));
    const totalSize = varint.encodingLength(currentSealVersion) + emptyRangesSize.length + serializedEmptyRanges.length + (this.keys.length * tcrypto.SYMMETRIC_KEY_SIZE);

    const buf = new Uint8Array(totalSize);
    varint.encode(currentSealVersion, buf);
    let offset = varint.encode.bytes;
    offset = Serialize.setStaticArray(emptyRangesSize, buf, offset);
    offset = Serialize.setStaticArray(serializedEmptyRanges, buf, offset);
    for (let i = 0; i < this.keys.length; i++)
      offset = Serialize.setStaticArray(this.keys[i], buf, offset);
    return buf;
  }