blob: 8e2f128c6a9f647cc011bda44b9a701177caa37d [file] [log] [blame]
[email protected]c322aa92011-01-27 11:21:071// 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
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/secure_hash.h"
[email protected]c322aa92011-01-27 11:21:076
7#include <openssl/ssl.h>
8
9#include "base/basictypes.h"
10#include "base/logging.h"
[email protected]4b559b4d2011-04-14 17:37:1411#include "crypto/openssl_util.h"
[email protected]c322aa92011-01-27 11:21:0712
[email protected]4b559b4d2011-04-14 17:37:1413namespace crypto {
[email protected]c322aa92011-01-27 11:21:0714
15namespace {
16
17class 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
43SecureHash* SecureHash::Create(Algorithm algorithm) {
44 switch (algorithm) {
45 case SHA256:
46 return new SecureHashSHA256OpenSSL();
47 default:
48 NOTIMPLEMENTED();
49 return NULL;
50 }
51}
52
[email protected]4b559b4d2011-04-14 17:37:1453} // namespace crypto