How to use the @tanker/crypto.random 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 / Groups / __tests__ / Verification.spec.js View on Github external
beforeEach(async () => {
      testGenerator.makeTrustchainCreation();
      const userId = random(tcrypto.HASH_SIZE);
      const userCreation = await testGenerator.makeUserCreation(userId);
      user = userCreation.user;
      const provisionalIdentity = testGenerator.makeProvisionalUser().publicProvisionalUser;
      const userGroupCreation = testGenerator.makeUserGroupCreation(userCreation, [user], [provisionalIdentity]);
      group = userGroupCreation.group;

      // Second user
      const userId2 = random(tcrypto.HASH_SIZE);
      const userCreation2 = await testGenerator.makeUserCreation(userId2);
      const userGroupAddition = testGenerator.makeUserGroupAddition(userCreation, userGroupCreation.group, [userCreation2.user]);

      userGroupEntry = userGroupAddition.userGroupEntry;
    });
github TankerHQ / sdk-js / packages / core / src / DataProtection / ChunkEncryptor.js View on Github external
if (typeof index !== 'number')
      throw new InvalidArgument('index', 'number', index);

    if (!(clearData instanceof Uint8Array))
      throw new InvalidArgument('clearData', 'Uint8Array', clearData);

    if (index < 0)
      throw new ChunkIndexOutOfRange(index, this.chunkKeys.length);

    if (index > this.chunkKeys.length) {
      const rangeSize = index - 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[index] = ephemeralKey;
    return encryptedData;
  }
github TankerHQ / sdk-js / packages / core / src / __tests__ / BlockGenerator.spec.js View on Github external
it('order stuff correctly for UserGroupAdditionV2 sign data', async () => {
    const record: UserGroupAdditionRecordV2 = {
      group_id: makeUint8Array('group id', tcrypto.HASH_SIZE),
      previous_group_block: makeUint8Array('prev group block', tcrypto.HASH_SIZE),
      encrypted_group_private_encryption_keys_for_users: [
        {
          user_id: random(tcrypto.HASH_SIZE),
          public_user_encryption_key: makeUint8Array('user pub enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
          encrypted_group_private_encryption_key: makeUint8Array('enc group priv enc key', tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE),
        },
        {
          user_id: random(tcrypto.HASH_SIZE),
          public_user_encryption_key: makeUint8Array('user pub enc key 2', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
          encrypted_group_private_encryption_key: makeUint8Array('enc group priv enc key 2', 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),
        },
      ],
      self_signature_with_current_key: new Uint8Array(0),
github TankerHQ / sdk-js / packages / core / src / __tests__ / TrustchainPuller.spec.js View on Github external
function makeMockClient() {
  return {
    _callbacks: { blockAvailable: [], started: [], invalid: [] },
    _send: async (/*event, payload*/) => { expect.fail(true, false, 'this method should have been called in the tests'); },
    emit: function emit(event) { this._callbacks[event].forEach(cb => cb()); },
    on: function on(event, cb) { this._callbacks[event].push(cb); },
    socket: true,
    trustchainId: random(32),
  };
}
github TankerHQ / sdk-js / packages / core / src / __tests__ / UsersSerialize.js View on Github external
it('should serialize/unserialize a DeviceRevocation', async () => {
    const deviceRevocation = {
      device_id: random(tcrypto.HASH_SIZE),
      user_keys: {
        public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
        previous_public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
        encrypted_previous_encryption_key: random(tcrypto.SEALED_KEY_SIZE),
        private_keys: [],
      },
    };

    expect(unserializeDeviceRevocationV2(serializeDeviceRevocationV2(deviceRevocation))).to.deep.equal(deviceRevocation);
  });
github TankerHQ / sdk-js / packages / core / src / __tests__ / SessionOpener.spec.js View on Github external
before(() => {
    trustchainId = random(tcrypto.HASH_SIZE);
    trustchainKeyPair = tcrypto.makeSignKeyPair();
  });
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__ / BlockGenerator.spec.js View on Github external
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),
        },
      ],
      self_signature: new Uint8Array(0),
    };

    const expectedSignData = utils.concatArrays(
      record.public_signature_key,
      record.public_encryption_key,
      record.encrypted_group_private_signature_key,
      record.encrypted_group_private_encryption_keys_for_users[0].user_id,
      record.encrypted_group_private_encryption_keys_for_users[0].public_user_encryption_key,
      record.encrypted_group_private_encryption_keys_for_users[0].encrypted_group_private_encryption_key,
      record.encrypted_group_private_encryption_keys_for_users[1].user_id,
      record.encrypted_group_private_encryption_keys_for_users[1].public_user_encryption_key,
github TankerHQ / sdk-js / packages / identity / src / userSecret.js View on Github external
export function createUserSecretBinary(trustchainId: b64string, userId: string): Uint8Array {
  if (typeof trustchainId !== 'string')
    throw new Error('Assertion error: invalid Trustchain ID.');

  if (typeof userId !== 'string')
    throw new Error('Assertion error: invalid user ID.');

  const rand = random(USER_SECRET_SIZE - 1);
  const checkByte = checksumByte(rand, obfuscateUserId(fromBase64(trustchainId), userId));
  const secret = new Uint8Array(USER_SECRET_SIZE);
  secret.set(rand);
  secret[USER_SECRET_SIZE - 1] = checkByte;
  return secret;
}