Index: chrome/browser/sync/util/cryptographer.cc |
diff --git a/chrome/browser/sync/util/cryptographer.cc b/chrome/browser/sync/util/cryptographer.cc |
index 59e96103ba71c56891b7dc3a0baf41e69f35a40a..9db70c85cb7fd48444418a038bc156f8d17416e4 100644 |
--- a/chrome/browser/sync/util/cryptographer.cc |
+++ b/chrome/browser/sync/util/cryptographer.cc |
@@ -57,9 +57,36 @@ bool Cryptographer::CanDecryptUsingDefaultKey( |
return default_nigori_ && (data.key_name() == default_nigori_->first); |
} |
+bool Cryptographer::EncryptIfDifferent( |
+ const ::google::protobuf::MessageLite& message, |
+ sync_pb::EncryptedData* encrypted) const { |
+ DCHECK(encrypted); |
+ if (!default_nigori_) { |
+ LOG(ERROR) << "Cryptographer not ready, failed to encrypt."; |
+ return false; |
+ } |
+ |
+ std::string serialized; |
+ if (!message.SerializeToString(&serialized)) { |
+ LOG(ERROR) << "Message is invalid/missing a required field."; |
+ return false; |
+ } |
+ |
+ if (CanDecryptUsingDefaultKey(*encrypted)) { |
+ std::string original_serialized = DecryptToString(*encrypted); |
akalin
2011/12/09 23:52:42
const ref
Nicolas Zea
2011/12/12 20:12:26
Done.
|
+ if (original_serialized == serialized) { |
+ DVLOG(2) << "Re-encryption unnecessary, encrypted data already matches."; |
+ return true; |
+ } |
+ } |
+ |
+ return EncryptImpl(serialized, default_nigori_, encrypted); |
+} |
+ |
bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, |
sync_pb::EncryptedData* encrypted) const { |
- if (!encrypted || !default_nigori_) { |
+ DCHECK(encrypted); |
+ if (!default_nigori_) { |
LOG(ERROR) << "Cryptographer not ready, failed to encrypt."; |
return false; |
} |
@@ -70,9 +97,15 @@ bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, |
return false; |
} |
+ return EncryptImpl(serialized, default_nigori_, encrypted); |
+} |
+ |
+bool Cryptographer::EncryptImpl(const std::string& serialized, |
+ const NigoriMap::value_type* nigori, |
+ sync_pb::EncryptedData* encrypted) const { |
encrypted->set_key_name(default_nigori_->first); |
- if (!default_nigori_->second->Encrypt(serialized, |
- encrypted->mutable_blob())) { |
+ if (!nigori->second->Encrypt(serialized, |
+ encrypted->mutable_blob())) { |
LOG(ERROR) << "Failed to encrypt data."; |
return false; |
} |
@@ -119,7 +152,7 @@ bool Cryptographer::GetKeys(sync_pb::EncryptedData* encrypted) const { |
} |
// Encrypt the bag with the default Nigori. |
- return Encrypt(bag, encrypted); |
+ return EncryptIfDifferent(bag, encrypted); |
} |
bool Cryptographer::AddKey(const KeyParams& params) { |