Home | History | Annotate | Download | only in crypto
      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
      3 // found in the LICENSE file.
      4 
      5 #include "crypto/secure_hash.h"
      6 
      7 #include <openssl/ssl.h>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/logging.h"
     11 #include "crypto/openssl_util.h"
     12 
     13 namespace crypto {
     14 
     15 namespace {
     16 
     17 class SecureHashSHA256OpenSSL : public SecureHash {
     18  public:
     19   SecureHashSHA256OpenSSL() {
     20     SHA256_Init(&ctx_);
     21   }
     22 
     23   virtual ~SecureHashSHA256OpenSSL() {
     24     OPENSSL_cleanse(&ctx_, sizeof(ctx_));
     25   }
     26 
     27   virtual void Update(const void* input, size_t len) {
     28     SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
     29   }
     30 
     31   virtual void Finish(void* output, size_t len) {
     32     ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
     33         static_cast<unsigned char*>(output), len);
     34     SHA256_Final(result.safe_buffer(), &ctx_);
     35   }
     36 
     37  private:
     38   SHA256_CTX ctx_;
     39 };
     40 
     41 }  // namespace
     42 
     43 SecureHash* SecureHash::Create(Algorithm algorithm) {
     44   switch (algorithm) {
     45     case SHA256:
     46       return new SecureHashSHA256OpenSSL();
     47     default:
     48       NOTIMPLEMENTED();
     49       return NULL;
     50   }
     51 }
     52 
     53 }  // namespace crypto
     54