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"
9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/pickle.h"
12 #include "crypto/openssl_util.h"
13 #include "third_party/boringssl/src/include/openssl/mem.h"
14 #include "third_party/boringssl/src/include/openssl/sha.h"
20 class SecureHashSHA256 : public SecureHash {
26 SecureHashSHA256(const SecureHashSHA256& other) {
27 memcpy(&ctx_, &other.ctx_, sizeof(ctx_));
30 ~SecureHashSHA256() override {
31 OPENSSL_cleanse(&ctx_, sizeof(ctx_));
34 void Update(const void* input, size_t len) override {
35 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
38 void Finish(void* output, size_t len) override {
39 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
40 static_cast<unsigned char*>(output), len);
41 SHA256_Final(result.safe_buffer(), &ctx_);
44 std::unique_ptr<SecureHash> Clone() const override {
45 return std::make_unique<SecureHashSHA256>(*this);
48 size_t GetHashLength() const override { return SHA256_DIGEST_LENGTH; }
56 std::unique_ptr<SecureHash> SecureHash::Create(Algorithm algorithm) {
59 return std::make_unique<SecureHashSHA256>();