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..047a6e9120de004fc139d1fea40ed48054e16bc0 100644 |
--- a/chrome/browser/sync/util/cryptographer.cc |
+++ b/chrome/browser/sync/util/cryptographer.cc |
@@ -57,9 +57,11 @@ bool Cryptographer::CanDecryptUsingDefaultKey( |
return default_nigori_ && (data.key_name() == default_nigori_->first); |
} |
-bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, |
- sync_pb::EncryptedData* encrypted) const { |
- if (!encrypted || !default_nigori_) { |
+bool Cryptographer::Encrypt( |
+ 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; |
} |
@@ -70,9 +72,23 @@ bool Cryptographer::Encrypt(const ::google::protobuf::MessageLite& message, |
return false; |
} |
+ if (CanDecryptUsingDefaultKey(*encrypted)) { |
+ const std::string& original_serialized = DecryptToString(*encrypted); |
+ if (original_serialized == serialized) { |
+ DVLOG(2) << "Re-encryption unnecessary, encrypted data already matches."; |
+ return true; |
+ } |
+ } |
+ |
+ return EncryptImpl(serialized, default_nigori_, encrypted); |
akalin
2011/12/12 22:05:22
do we still need the Encrypt/EncryptImpl divide?
Nicolas Zea
2011/12/13 00:43:30
Done.
|
+} |
+ |
+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; |
} |