Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(602)

Unified Diff: crypto/secure_hash_default.cc

Issue 8588057: Added serialization to SecureHash. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added a serialization unit test for SecureHash. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: crypto/secure_hash_default.cc
diff --git a/crypto/secure_hash_default.cc b/crypto/secure_hash_default.cc
index d85754a8e37d8ccbddcc0eaf64caf6c13dca5a70..d57affdade71551dc2aa3b23b717e2bed4a3e2a6 100644
--- a/crypto/secure_hash_default.cc
+++ b/crypto/secure_hash_default.cc
@@ -5,6 +5,7 @@
#include "crypto/secure_hash.h"
#include "base/logging.h"
+#include "base/pickle.h"
#include "crypto/third_party/nss/chromium-blapi.h"
#include "crypto/third_party/nss/chromium-sha256.h"
@@ -14,6 +15,9 @@ namespace {
class SecureHashSHA256NSS : public SecureHash {
public:
+ static const int kSecureHashVersion = 1;
+ static const char* kDescriptor;
+
SecureHashSHA256NSS() {
SHA256_Begin(&ctx_);
}
@@ -30,10 +34,55 @@ class SecureHashSHA256NSS : public SecureHash {
static_cast<unsigned int>(len));
}
+ virtual bool Serialize(Pickle* pickle);
+ virtual bool Deserialize(Pickle* pickle);
+
private:
SHA256Context ctx_;
};
+const char* SecureHashSHA256NSS::kDescriptor = "NSS";
+
+bool SecureHashSHA256NSS::Serialize(Pickle* pickle) {
+ if (!pickle)
+ return false;
+
+ if (!pickle->WriteInt(kSecureHashVersion))
+ return false;
+ if (!pickle->WriteString(kDescriptor))
+ return false;
+ if (!pickle->WriteBytes(&ctx_, sizeof(ctx_)))
+ return false;
+
+ return true;
+}
+
+bool SecureHashSHA256NSS::Deserialize(Pickle* pickle) {
+ if (!pickle)
+ return false;
+
+ void* data_iterator = NULL;
+ int version;
+ if (!pickle->ReadInt(&data_iterator, &version))
+ return false;
+
+ if (version > kSecureHashVersion)
+ return false; // We don't know how to deal with this.
+
+ std::string type;
+ if (!pickle->ReadString(&data_iterator, &type))
+ return false;
+
+ if (type != kDescriptor)
+ return false; // It's the wrong kind.
+
+ const char* data = reinterpret_cast<char*>(&ctx_);
+ if (!pickle->ReadBytes(&data_iterator, &data, sizeof(ctx_)))
+ return false;
+
+ return true;
+}
+
} // namespace
SecureHash* SecureHash::Create(Algorithm algorithm) {

Powered by Google App Engine
This is Rietveld 408576698