How to use the @tanker/crypto.tcrypto.HASH_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 / __tests__ / Generator.js View on Github external
let authorPrivateKey = this.appSignKeys.privateKey;
    let author = this.root.entry.hash;
    if (args.parentDevice) {
      // A parent device exists so we are in the add Device case
      authorPrivateKey = args.parentDevice.signKeys.privateKey;
      author = args.parentDevice.id;
    }
    let userKeyPair = null;
    if (args.nature === NATURE.device_creation_v3) {
      userKeyPair = {
        public_encryption_key: args.userKeys.publicKey,
        encrypted_private_encryption_key: new Uint8Array(SEALED_KEY_SIZE),
      };
    }
    const payload: DeviceCreationRecord = {
      last_reset: new Uint8Array(tcrypto.HASH_SIZE),
      ephemeral_public_signature_key: ephemeralKeys.publicKey,
      user_id: obfuscatedUserId,
      delegation_signature: tcrypto.sign(delegationBuffer, authorPrivateKey),
      public_signature_key: signKeys.publicKey,
      public_encryption_key: encryptionKeys.publicKey,
      is_ghost_device: false,
      revoked: Number.MAX_SAFE_INTEGER,
      user_key_pair: userKeyPair,
    };
    this.trustchainIndex += 1;

    let serializedPayload = null;
    if (args.nature === NATURE.device_creation_v3) {
      serializedPayload = serializeUserDeviceV3(payload);
    } else {
      serializedPayload = serializeUserDeviceV1(payload);
github TankerHQ / sdk-js / packages / core / src / __tests__ / TrustchainVerifier.spec.js View on Github external
it('should reject a key publish to device with a null recipient', async () => {
      const kp = await builder.generator.newKeyPublishToDevice({ toDevice: userV1.device, fromDevice: author.device });
      const newRecipient = new Uint8Array(tcrypto.HASH_SIZE);
      const newKp = setRecipientKeyPublish(kp, newRecipient, NATURE.key_publish_to_device);

      const verif = async () => {
        builder.trustchainVerifier._verifyKeyPublish(newKp.unverifiedKeyPublish, verifiedAuthorDevice);
      };

      await assertFailsWithNature(verif(), 'invalid_recipient');
    });
github TankerHQ / sdk-js / packages / core / src / __tests__ / UsersManagerHelper.spec.js View on Github external
it('can inflate multiple users from different blocks', async () => {
      const userId = random(tcrypto.HASH_SIZE);
      const userId2 = random(tcrypto.HASH_SIZE);

      const testGenerator = new TestGenerator();
      const trustchainCreation = testGenerator.makeTrustchainCreation();

      const userCreation = await testGenerator.makeUserCreation(userId);
      const deviceCreation = await testGenerator.makeDeviceCreation(userCreation);
      const deviceCreation2 = await testGenerator.makeDeviceCreation(deviceCreation);
      const deviceRevocation = await testGenerator.makeDeviceRevocation(deviceCreation2, deviceCreation2.testDevice.id);

      const userCreation2 = await testGenerator.makeUserCreation(userId2);
      const deviceCreationUser2 = await testGenerator.makeDeviceCreation(userCreation2);

      const blocks = [userCreation.block, userCreation2.block, deviceCreation.block, deviceCreationUser2.block, deviceCreation2.block, deviceRevocation.block];

      const { userIdToUserMap, deviceIdToUserIdMap } = await usersFromBlocks(blocks, trustchainCreation.trustchainKeys.publicKey);
github TankerHQ / sdk-js / packages / core / src / __tests__ / KeyPublishSerialize.spec.js View on Github external
it('correctly deserializes a KeyPublishV2 test vector', async () => {
    const keyPublish = {
      recipient: makeUint8Array('recipient user', tcrypto.HASH_SIZE),
      resourceId: makeUint8Array('resource mac', tcrypto.MAC_SIZE),
      key: makeUint8Array('encrypted key...', tcrypto.SEALED_KEY_SIZE),
    };

    const payload = new Uint8Array([
      0x72, 0x65, 0x63, 0x69, 0x70, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x75, 0x73, 0x65, 0x72, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x20, 0x6d, 0x61, 0x63, 0x00, 0x00, 0x00, 0x00,
      0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x20, 0x6b, 0x65, 0x79, 0x2e, 0x2e, 0x2e,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ]);

    expect(unserializeKeyPublish(payload)).to.deep.equal(keyPublish);
github TankerHQ / sdk-js / packages / core / src / __tests__ / payloads.spec.js View on Github external
it('should throw when unserializing unsupported block version', async () => {
    const block = {
      author: random(tcrypto.HASH_SIZE),
      signature: random(tcrypto.SIGNATURE_SIZE),
      trustchain_id: random(tcrypto.HASH_SIZE),
      payload: new Uint8Array(0),
      nature: NATURE_KIND.key_publish_to_device,
    };
    const serializedBlock = serializeBlock(block);
    serializedBlock[0] = 99;
    expect(() => unserializeBlock(serializedBlock)).to.throw(UpgradeRequiredError);
  });
github TankerHQ / sdk-js / packages / core / src / __tests__ / BlockGenerator.spec.js View on Github external
it('order stuff correctly for UserGroupCreationV2 sign data', async () => {
    const record = {
      public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
      public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
      encrypted_group_private_signature_key: random(tcrypto.SEALED_SIGNATURE_PRIVATE_KEY_SIZE),
      encrypted_group_private_encryption_keys_for_users: [
        {
          user_id: random(tcrypto.HASH_SIZE),
          public_user_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
          encrypted_group_private_encryption_key: random(tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE),
        },
        {
          user_id: random(tcrypto.HASH_SIZE),
          public_user_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
          encrypted_group_private_encryption_key: random(tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE),
        }
      ],
      encrypted_group_private_encryption_keys_for_provisional_users: [
        {
          app_provisional_user_public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
          tanker_provisional_user_public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
          encrypted_group_private_encryption_key: random(TWO_TIMES_SEALED_KEY_SIZE),
        },
      ],
github TankerHQ / sdk-js / packages / core / src / __tests__ / UnverifiedStore.spec.js View on Github external
before(async () => {
      userId = random(tcrypto.HASH_SIZE);
      const userCreation = await testGenerator.makeUserCreation(userId);
      claim = testGenerator.makeProvisionalIdentityClaim(userCreation, userId, random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE));

      await unverifiedStore.addUnverifiedProvisionalIdentityClaimEntries([claim.unverifiedProvisionalIdentityClaim]);
    });
github TankerHQ / sdk-js / packages / core / src / __tests__ / GroupManager.spec.js View on Github external
beforeEach(async () => {
    testUserCreation = await testGenerator.makeUserCreation(random(tcrypto.HASH_SIZE));
    testGroup = testGenerator.makeUserGroupCreation(testUserCreation, [testUserCreation.user]);
    await groupStore.put(testGroup.group);
    groupId = testGroup.group.groupId;
  });
github TankerHQ / sdk-js / packages / core / src / LocalUser / UserCreation.js View on Github external
) => {
  const ephemeralKeys = tcrypto.makeSignKeyPair();
  const delegationBuffer = utils.concatArrays(ephemeralKeys.publicKey, userId);

  const encryptedUserKeyForNewDevice = tcrypto.sealEncrypt(
    userKeys.privateKey,
    deviceEncryptionKeyPair.publicKey
  );

  const payload = serializeUserDeviceV3({
    ephemeral_public_signature_key: ephemeralKeys.publicKey,
    user_id: userId,
    delegation_signature: tcrypto.sign(delegationBuffer, ghostDevice.privateSignatureKey),
    public_signature_key: deviceSignatureKeyPair.publicKey,
    public_encryption_key: deviceEncryptionKeyPair.publicKey,
    last_reset: new Uint8Array(tcrypto.HASH_SIZE),
    user_key_pair: {
      public_encryption_key: userKeys.publicKey,
      encrypted_private_encryption_key: encryptedUserKeyForNewDevice,
    },
    is_ghost_device: false,
    revoked: Number.MAX_SAFE_INTEGER,
  });

  return createBlock(
    payload,
    preferredNature(NATURE_KIND.device_creation),
    trustchainId,
    ghostDeviceId,
    ephemeralKeys.privateKey
  ).block;
};
github TankerHQ / sdk-js / packages / core / src / LocalUser / UserCreation.js View on Github external
ghostDeviceKeys: GhostDeviceKeys,
  delegationToken: DelegationToken
) => {
  const userKeys = tcrypto.makeEncryptionKeyPair();
  const encryptedUserKey = tcrypto.sealEncrypt(
    userKeys.privateKey,
    ghostDeviceKeys.encryptionKeyPair.publicKey,
  );

  const ghostDevicePayload = serializeUserDeviceV3({
    ephemeral_public_signature_key: delegationToken.ephemeral_public_signature_key,
    user_id: userId,
    delegation_signature: delegationToken.delegation_signature,
    public_signature_key: ghostDeviceKeys.signatureKeyPair.publicKey,
    public_encryption_key: ghostDeviceKeys.encryptionKeyPair.publicKey,
    last_reset: new Uint8Array(tcrypto.HASH_SIZE),
    user_key_pair: {
      public_encryption_key: userKeys.publicKey,
      encrypted_private_encryption_key: encryptedUserKey,
    },
    is_ghost_device: true,
    revoked: Number.MAX_SAFE_INTEGER,
  });

  const { block, hash } = createBlock(
    ghostDevicePayload,
    preferredNature(NATURE_KIND.device_creation),
    trustchainId,
    trustchainId,
    delegationToken.ephemeral_private_signature_key
  );