[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 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 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/secure_hash.h" |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 6 | |
7 | #include <openssl/ssl.h> | ||||
8 | |||||
9 | #include "base/basictypes.h" | ||||
10 | #include "base/logging.h" | ||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 11 | #include "crypto/openssl_util.h" |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 12 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 13 | namespace crypto { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 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 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 53 | } // namespace crypto |