Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("wrong order", async () => {
const ciphers = await createCiphers();
const { Alice, Bob } = ciphers;
const AliceMsg1 = await Alice.encrypt(MESSAGE) as MessageSignedProtocol; // slow message
await dialog(Alice, Bob, 5);
assert.isTrue(isEqual(await Bob.decrypt(AliceMsg1), MESSAGE));
await dialog(Alice, Bob, 5);
});
it("import/export", async () => {
const identity = await createIdentity(1);
const identityProto = await IdentityProtocol.fill(identity);
const identityBuf = await identityProto.exportProto();
const identityProto2 = await IdentityProtocol.importProto(identityBuf);
assert.equal(identityProto.signingKey.id, identityProto2.signingKey.id);
assert.equal(identityProto.exchangeKey.id, identityProto2.exchangeKey.id);
assert.isTrue(isEqual(identityProto.signature, identityProto2.signature));
});
it("toJSON/fromJSON", async () => {
const res = await createIdentity(1);
const identity = await IdentityProtocol.fill(res);
const remote = await RemoteIdentity.fill(identity);
const json = await remote.toJSON();
const remote2 = await RemoteIdentity.fromJSON(json);
assert.equal(remote.createdAt.toString(), remote2.createdAt.toString());
assert.equal(remote.signingKey.id, remote2.signingKey.id);
assert.equal(remote.exchangeKey.id, remote2.exchangeKey.id);
assert.isTrue(isEqual(remote.signature, remote2.signature));
assert.equal(remote.id, remote2.id);
});
it("import/export", async () => {
const preKeySigned = new PreKeySignedProtocol();
let preKeySigned2: PreKeySignedProtocol;
const identity = await createIdentity(1);
preKeySigned.id = identity.id;
preKeySigned.key = identity.signingKey.publicKey;
await preKeySigned.sign(identity.signingKey.privateKey);
const protocol = await preKeySigned.exportProto();
const preKey = await PreKeySignedProtocol.importProto(protocol);
preKeySigned2 = preKey;
isEqual(preKeySigned.signature, preKeySigned2.signature);
const ok = await preKeySigned2.verify(identity.signingKey.publicKey);
assert.isTrue(ok);
});
async function createCiphers() {
const AliceID = await createIdentity(1);
const BobID = await createIdentity(2);
const AlicePreKeyBundle = await createPreKeyBundle(AliceID);
const BobRatchet = await AsymmetricRatchet.create(BobID, AlicePreKeyBundle);
const HelloMessage = await BobRatchet.encrypt(MESSAGE) as PreKeyMessageProtocol;
const AliceRatchet = await AsymmetricRatchet.create(AliceID, HelloMessage);
const decrypted = await AliceRatchet.decrypt(HelloMessage.signedMessage);
assert.isTrue(isEqual(decrypted, MESSAGE));
return { Alice: AliceRatchet, Bob: BobRatchet };
}
async function sendMessage(fromCipher: AsymmetricRatchet, toCipher: AsymmetricRatchet) {
const message = await fromCipher.encrypt(MESSAGE) as MessageSignedProtocol;
assert.isTrue(isEqual(await toCipher.decrypt(message), MESSAGE));
}
public async isEqual(other: any) {
if (!(other && other instanceof ECPublicKey)) { return false; }
return isEqual(this.serialized, other.serialized);
}
public async verify(hmacKey: CryptoKey) {
const signature = await this.signHMAC(hmacKey);
return utils.isEqual(signature, this.signature);
}