Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const BobRatchet = await AsymmetricRatchet.create(BobID, AlicePreKeyBundle);
const BobMessage1 = await BobRatchet.encrypt(Convert.FromUtf8String(messageText));
const BobMessage2 = await BobRatchet.encrypt(Convert.FromUtf8String(messageText));
// Only first message mast be PreKeyMessage
assert.isTrue(BobMessage1 instanceof PreKeyMessageProtocol);
assert.isTrue(BobMessage2 instanceof MessageSignedProtocol);
const AliceRatchet = await AsymmetricRatchet.create(AliceID, BobMessage1 as PreKeyMessageProtocol);
let decrypted = await AliceRatchet.decrypt((BobMessage1 as PreKeyMessageProtocol).signedMessage);
assert.equal(messageText, Convert.ToUtf8String(decrypted));
decrypted = await AliceRatchet.decrypt((BobMessage2 as MessageSignedProtocol));
assert.equal(messageText, Convert.ToUtf8String(decrypted));
});
const BobID = await createIdentity(2);
const AlicePreKeyBundle = await createPreKeyBundle(AliceID);
const messageText = "Hello world!!";
const BobRatchet = await AsymmetricRatchet.create(BobID, AlicePreKeyBundle);
const BobMessage1 = await BobRatchet.encrypt(Convert.FromUtf8String(messageText));
const BobMessage2 = await BobRatchet.encrypt(Convert.FromUtf8String(messageText));
// Only first message mast be PreKeyMessage
assert.isTrue(BobMessage1 instanceof PreKeyMessageProtocol);
assert.isTrue(BobMessage2 instanceof MessageSignedProtocol);
const AliceRatchet = await AsymmetricRatchet.create(AliceID, BobMessage1 as PreKeyMessageProtocol);
let decrypted = await AliceRatchet.decrypt((BobMessage1 as PreKeyMessageProtocol).signedMessage);
assert.equal(messageText, Convert.ToUtf8String(decrypted));
decrypted = await AliceRatchet.decrypt((BobMessage2 as MessageSignedProtocol));
assert.equal(messageText, Convert.ToUtf8String(decrypted));
});
private fixNativeResult(method: SubtleMethods, args: any[], res: any): any {
if (method === "exportKey") {
if (args[0]?.toLowerCase?.() === "jwk" && res instanceof ArrayBuffer) {
// IE11 uses ArrayBuffer instead of JSON object
return JSON.parse(Convert.ToUtf8String(res));
}
}
if (this.browserInfo.name === Browser.IE) {
// wrap IE11 native key
if ("privateKey" in res) {
const privateKeyUsages = ["sign", "decrypt", "unwrapKey", "deriveKey", "deriveBits"];
const publicKeyUsages = ["verify", "encrypt", "wrapKey"];
return {
privateKey: this.wrapNativeKey(res.privateKey, args[0], args[1], args[2].filter((o: string) => privateKeyUsages.includes(o))),
publicKey: this.wrapNativeKey(res.publicKey, args[0], args[1], args[2].filter((o: string) => publicKeyUsages.includes(o))),
};
} else if ("extractable" in res) {
return this.wrapNativeKey(res, method === "importKey" ? args[2] : args[4], res.extractable, method === "importKey" ? args[4] : args[6]);
}
}
const data = await this.exportKey(args[0], args[1]);
const keyData = (args[0] === "jwk") ? Convert.FromUtf8String(JSON.stringify(data)) : data;
const res = await this.encrypt(args[3], args[2], keyData);
return res;
} catch (e) {
Debug.warn(`Cannot wrap key by native functions. ${e.message}`, e);
}
}
if (method === "unwrapKey") {
try {
Debug.info(`Trying to unwrap key by using native functions`, args);
// unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages);
// indexes 0 1 2 3 4 5 6
const data = await this.decrypt(args[3], args[2], args[1]);
const keyData = (args[0] === "jwk") ? JSON.parse(Convert.ToUtf8String(data)) : data;
const res = await this.importKey(args[0], keyData, args[4], args[5], args[6]);
return res;
} catch (e) {
Debug.warn(`Cannot unwrap key by native functions. ${e.message}`, e);
}
}
if (method === "deriveKey") {
try {
Debug.info(`Trying to derive key by using native functions`, args);
const data = await this.deriveBits(args[0], args[1], args[2].length);
const res = await this.importKey("raw", data, args[2], args[3], args[4]);
return res;
} catch (e) {
Debug.warn(`Cannot derive key by native functions. ${e.message}`, e);
}
.then((bytes) => {
console.log("Bob's decrypted message:", Convert.ToUtf8String(bytes));
})
});
public static async get(value: Uint8Array) {
return Convert.ToUtf8String(value);
}
}