In my custom function, I have a promise-like object that decrypts a given message using the web crypto API. The issue is that in my decryption function, I need to test several different values as input and run this promise-like object multiple times in a for loop. Ultimately, I want the function to return whichever of these promises successfully resolves.
public decryptMessage(latitude: [string, number], longitude: [string, number], ciphertext: string) {
// Retrieve salt
const salt = localStorage.getItem("salt");
const retrievedSaltArray = JSON.parse(salt);
const saltBytes = new Uint8Array(retrievedSaltArray);
// Retrieve IV
const iv = localStorage.getItem("iv");
const retrievedIvArray = JSON.parse(iv);
const ivBytes = new Uint8Array(retrievedIvArray);
// Get tolerance distance
let toleranceDistance = parseInt(JSON.parse(localStorage.getItem("toleranceDistance")));
// Get original keyHash
let originalHash = localStorage.getItem("keyhash");
// Create location inputs (locations with adjacent quadrants)
let location = new Location(latitude, longitude);
let locationInputs = location.prepareReceiverLocationInputs();
let encryptionTool = new EncryptionHelper(saltBytes, ivBytes);
for (let i = 0; i <= locationInputs.length - 1; i++) {
let plaintText = encryptionTool.decrypt(locationInputs[i], ciphertext, originalHash);
plaintText.then(function(plaintTextResult) {
return plaintTextResult;
});
}
}
Essentially, I am trying to run the encryptionTool.decrypt() method in a for loop so that the return value of the decryptMessage method is whichever promise resolves successfully. However, since this method uses the webcrypto API, it does not have reject or catch methods as it is a promise-like method.