[NaClDocs] Fill in the landing page for the SDK section of the docs.
[chromium-blink-merge.git] / crypto / secure_hash_default.cc
blob7b912e1af60bfc09af68f9dd5e88ddcd9a40f3fd
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() {
27 memset(&ctx_, 0, sizeof(ctx_));
30 // SecureHash implementation:
31 virtual void Update(const void* input, size_t len) OVERRIDE {
32 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
35 virtual void Finish(void* output, size_t len) OVERRIDE {
36 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL,
37 static_cast<unsigned int>(len));
40 virtual bool Serialize(Pickle* pickle) OVERRIDE;
41 virtual bool Deserialize(PickleIterator* data_iterator) OVERRIDE;
43 private:
44 SHA256Context ctx_;
47 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) {
48 if (!pickle)
49 return false;
51 if (!pickle->WriteInt(kSecureHashVersion) ||
52 !pickle->WriteString(kSHA256Descriptor) ||
53 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) {
54 return false;
57 return true;
60 bool SecureHashSHA256NSS::Deserialize(PickleIterator* data_iterator) {
61 int version;
62 if (!data_iterator->ReadInt(&version))
63 return false;
65 if (version > kSecureHashVersion)
66 return false; // We don't know how to deal with this.
68 std::string type;
69 if (!data_iterator->ReadString(&type))
70 return false;
72 if (type != kSHA256Descriptor)
73 return false; // It's the wrong kind.
75 const char* data = NULL;
76 if (!data_iterator->ReadBytes(&data, sizeof(ctx_)))
77 return false;
79 memcpy(&ctx_, data, sizeof(ctx_));
81 return true;
84 } // namespace
86 SecureHash* SecureHash::Create(Algorithm algorithm) {
87 switch (algorithm) {
88 case SHA256:
89 return new SecureHashSHA256NSS();
90 default:
91 NOTIMPLEMENTED();
92 return NULL;
96 } // namespace crypto