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

Side by Side 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 iterator to Deserialize, applied comments from agl. 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 unified diff | Download patch
« no previous file with comments | « crypto/secure_hash.h ('k') | crypto/secure_hash_openssl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "crypto/secure_hash.h" 5 #include "crypto/secure_hash.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/pickle.h"
8 #include "crypto/third_party/nss/chromium-blapi.h" 9 #include "crypto/third_party/nss/chromium-blapi.h"
9 #include "crypto/third_party/nss/chromium-sha256.h" 10 #include "crypto/third_party/nss/chromium-sha256.h"
10 11
11 namespace crypto { 12 namespace crypto {
12 13
13 namespace { 14 namespace {
14 15
16 const char kSHA256Descriptor[] = "NSS";
17
15 class SecureHashSHA256NSS : public SecureHash { 18 class SecureHashSHA256NSS : public SecureHash {
16 public: 19 public:
20 static const int kSecureHashVersion = 1;
21
17 SecureHashSHA256NSS() { 22 SecureHashSHA256NSS() {
18 SHA256_Begin(&ctx_); 23 SHA256_Begin(&ctx_);
19 } 24 }
20 25
21 virtual ~SecureHashSHA256NSS() { 26 virtual ~SecureHashSHA256NSS() {
22 } 27 }
23 28
24 virtual void Update(const void* input, size_t len) { 29 virtual void Update(const void* input, size_t len) {
25 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); 30 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
26 } 31 }
27 32
28 virtual void Finish(void* output, size_t len) { 33 virtual void Finish(void* output, size_t len) {
29 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL, 34 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL,
30 static_cast<unsigned int>(len)); 35 static_cast<unsigned int>(len));
31 } 36 }
32 37
38 virtual bool Serialize(Pickle* pickle);
39 virtual bool Deserialize(void** data_iterator, Pickle* pickle);
40
33 private: 41 private:
34 SHA256Context ctx_; 42 SHA256Context ctx_;
35 }; 43 };
36 44
45 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) {
46 if (!pickle)
47 return false;
48
49 if (!pickle->WriteInt(kSecureHashVersion) ||
50 !pickle->WriteString(kSHA256Descriptor) ||
51 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) {
52 return false;
53 }
54
55 return true;
56 }
57
58 bool SecureHashSHA256NSS::Deserialize(void** data_iterator, Pickle* pickle) {
59 if (!pickle)
60 return false;
61
62 int version;
63 if (!pickle->ReadInt(data_iterator, &version))
64 return false;
65
66 if (version > kSecureHashVersion)
67 return false; // We don't know how to deal with this.
68
69 std::string type;
70 if (!pickle->ReadString(data_iterator, &type))
71 return false;
72
73 if (type != kSHA256Descriptor)
74 return false; // It's the wrong kind.
75
76 const char* data = NULL;
77 if (!pickle->ReadBytes(data_iterator, &data, sizeof(ctx_)))
78 return false;
79
80 memcpy(&ctx_, data, sizeof(ctx_));
81
82 return true;
83 }
84
37 } // namespace 85 } // namespace
38 86
39 SecureHash* SecureHash::Create(Algorithm algorithm) { 87 SecureHash* SecureHash::Create(Algorithm algorithm) {
40 switch (algorithm) { 88 switch (algorithm) {
41 case SHA256: 89 case SHA256:
42 return new SecureHashSHA256NSS(); 90 return new SecureHashSHA256NSS();
43 default: 91 default:
44 NOTIMPLEMENTED(); 92 NOTIMPLEMENTED();
45 return NULL; 93 return NULL;
46 } 94 }
47 } 95 }
48 96
49 } // namespace crypto 97 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/secure_hash.h ('k') | crypto/secure_hash_openssl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698