Fix bug in load time stats.
[chromium-blink-merge.git] / crypto / secure_hash_default.cc
blobb743b4c1c9615ec5f77bdf3a0bd0dab815c83764
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/secure_hash.h"
7 #include "base/logging.h"
8 #include "base/pickle.h"
9 #include "crypto/third_party/nss/chromium-blapi.h"
10 #include "crypto/third_party/nss/chromium-sha256.h"
12 namespace crypto {
14 namespace {
16 const char kSHA256Descriptor[] = "NSS";
18 class SecureHashSHA256NSS : public SecureHash {
19 public:
20 static const int kSecureHashVersion = 1;
22 SecureHashSHA256NSS() {
23 SHA256_Begin(&ctx_);
26 virtual ~SecureHashSHA256NSS() {
29 // SecureHash implementation:
30 virtual void Update(const void* input, size_t len) OVERRIDE {
31 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
34 virtual void Finish(void* output, size_t len) OVERRIDE {
35 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL,
36 static_cast<unsigned int>(len));
39 virtual bool Serialize(Pickle* pickle) OVERRIDE;
40 virtual bool Deserialize(PickleIterator* data_iterator) OVERRIDE;
42 private:
43 SHA256Context ctx_;
46 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) {
47 if (!pickle)
48 return false;
50 if (!pickle->WriteInt(kSecureHashVersion) ||
51 !pickle->WriteString(kSHA256Descriptor) ||
52 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) {
53 return false;
56 return true;
59 bool SecureHashSHA256NSS::Deserialize(PickleIterator* data_iterator) {
60 int version;
61 if (!data_iterator->ReadInt(&version))
62 return false;
64 if (version > kSecureHashVersion)
65 return false; // We don't know how to deal with this.
67 std::string type;
68 if (!data_iterator->ReadString(&type))
69 return false;
71 if (type != kSHA256Descriptor)
72 return false; // It's the wrong kind.
74 const char* data = NULL;
75 if (!data_iterator->ReadBytes(&data, sizeof(ctx_)))
76 return false;
78 memcpy(&ctx_, data, sizeof(ctx_));
80 return true;
83 } // namespace
85 SecureHash* SecureHash::Create(Algorithm algorithm) {
86 switch (algorithm) {
87 case SHA256:
88 return new SecureHashSHA256NSS();
89 default:
90 NOTIMPLEMENTED();
91 return NULL;
95 } // namespace crypto