- const vector<EncryptedKey*>& keys=m_ref.getEncryptedKeys();
- for (vector<EncryptedKey*>::const_iterator i=keys.begin(); i!=keys.end(); i++) {
- if (XMLString::equals(recipient,(*i)->getRecipient()))
- return (*i);
- }
- return nullptr;
+ static bool (*equal_fn)(const XMLCh*, const XMLCh*) = &XMLString::equals;
+
+ // Look for first match that has no Recipient attribute, or matches the input recipient.
+ // Using XMLString::equals allows for both to be NULL and still match.
+ vector<EncryptedKey*>::const_iterator k = find_if(
+ m_ref.getEncryptedKeys().begin(), m_ref.getEncryptedKeys().end(),
+ (lambda::bind(&EncryptedKey::getRecipient, _1) == ((const XMLCh*)nullptr) ||
+ lambda::bind(equal_fn, recipient, lambda::bind(&EncryptedKey::getRecipient, _1)))
+ );
+ return (k != m_ref.getEncryptedKeys().end()) ? (*k) : nullptr;