Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
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);
});
});
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);
});
});
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
};
}
beforeEach(() => {
key = random(tcrypto.SYMMETRIC_KEY_SIZE);
resourceId = random(16);
mapper = { findKey: () => Promise.resolve(key) };
stream = new DecryptorStream(mapper);
sync = watchStream(stream);
});
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
};
}
export function makeResource(): Resource {
const key = random(tcrypto.SYMMETRIC_KEY_SIZE);
const resourceId = generichash(key, tcrypto.MAC_SIZE);
return { key, resourceId };
}
makeStreamResource(): ResourceMeta {
const key = random(tcrypto.SYMMETRIC_KEY_SIZE);
const resourceId = generichash(key, tcrypto.MAC_SIZE);
return { key, resourceId };
}
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) };
}
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;
}