Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / crypto / secure_hash_default.cc
blob739b402b6c199de0e669d21a6c45a20ca9f0e31a
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 ~SecureHashSHA256NSS() override { memset(&ctx_, 0, sizeof(ctx_)); }
28 // SecureHash implementation:
29 void Update(const void* input, size_t len) override {
30 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
33 void Finish(void* output, size_t len) override {
34 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL,
35 static_cast<unsigned int>(len));
38 bool Serialize(base::Pickle* pickle) override;
39 bool Deserialize(base::PickleIterator* data_iterator) override;
41 private:
42 SHA256Context ctx_;
45 bool SecureHashSHA256NSS::Serialize(base::Pickle* pickle) {
46 if (!pickle)
47 return false;
49 if (!pickle->WriteInt(kSecureHashVersion) ||
50 !pickle->WriteString(kSHA256Descriptor) ||
51 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) {
52 return false;
55 return true;
58 bool SecureHashSHA256NSS::Deserialize(base::PickleIterator* data_iterator) {
59 int version;
60 if (!data_iterator->ReadInt(&version))
61 return false;
63 if (version > kSecureHashVersion)
64 return false; // We don't know how to deal with this.
66 std::string type;
67 if (!data_iterator->ReadString(&type))
68 return false;
70 if (type != kSHA256Descriptor)
71 return false; // It's the wrong kind.
73 const char* data = NULL;
74 if (!data_iterator->ReadBytes(&data, sizeof(ctx_)))
75 return false;
77 memcpy(&ctx_, data, sizeof(ctx_));
79 return true;
82 } // namespace
84 SecureHash* SecureHash::Create(Algorithm algorithm) {
85 switch (algorithm) {
86 case SHA256:
87 return new SecureHashSHA256NSS();
88 default:
89 NOTIMPLEMENTED();
90 return NULL;
94 } // namespace crypto