OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Utility class for calculating the HMAC for a given message. We currently | 5 // Utility class for calculating the HMAC for a given message. We currently |
6 // only support SHA1 for the hash algorithm, but this can be extended easily. | 6 // only support SHA1 for the hash algorithm, but this can be extended easily. |
7 | 7 |
8 #ifndef CRYPTO_HMAC_H_ | 8 #ifndef CRYPTO_HMAC_H_ |
9 #define CRYPTO_HMAC_H_ | 9 #define CRYPTO_HMAC_H_ |
10 #pragma once | 10 #pragma once |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
16 #include "crypto/crypto_export.h" | 16 #include "crypto/crypto_export.h" |
17 | 17 |
18 namespace crypto { | 18 namespace crypto { |
19 | 19 |
20 // Simplify the interface and reduce includes by abstracting out the internals. | 20 // Simplify the interface and reduce includes by abstracting out the internals. |
21 struct HMACPlatformData; | 21 struct HMACPlatformData; |
| 22 class SymmetricKey; |
22 | 23 |
23 class CRYPTO_EXPORT HMAC { | 24 class CRYPTO_EXPORT HMAC { |
24 public: | 25 public: |
25 // The set of supported hash functions. Extend as required. | 26 // The set of supported hash functions. Extend as required. |
26 enum HashAlgorithm { | 27 enum HashAlgorithm { |
27 SHA1, | 28 SHA1, |
28 SHA256, | 29 SHA256, |
29 }; | 30 }; |
30 | 31 |
31 explicit HMAC(HashAlgorithm hash_alg); | 32 explicit HMAC(HashAlgorithm hash_alg); |
32 ~HMAC(); | 33 ~HMAC(); |
33 | 34 |
34 // Returns the length of digest that this HMAC will create. | 35 // Returns the length of digest that this HMAC will create. |
35 size_t DigestLength() const; | 36 size_t DigestLength() const; |
36 | 37 |
37 // TODO(abarth): Add a PreferredKeyLength() member function. | 38 // TODO(abarth): Add a PreferredKeyLength() member function. |
38 | 39 |
39 // Initializes this instance using |key| of the length |key_length|. Call Init | 40 // Initializes this instance using |key| of the length |key_length|. Call Init |
40 // only once. It returns false on the second or later calls. | 41 // only once. It returns false on the second or later calls. |
41 // TODO(abarth): key_length should be a size_t. | 42 // TODO(abarth): key_length should be a size_t. |
42 bool Init(const unsigned char* key, int key_length) WARN_UNUSED_RESULT; | 43 bool Init(const unsigned char* key, int key_length) WARN_UNUSED_RESULT; |
43 | 44 |
| 45 // Initializes this instance using |key|. Call Init |
| 46 // only once. It returns false on the second or later calls. |
| 47 bool Init(SymmetricKey* key) WARN_UNUSED_RESULT; |
| 48 |
44 // Initializes this instance using |key|. Call Init only once. It returns | 49 // Initializes this instance using |key|. Call Init only once. It returns |
45 // false on the second or later calls. | 50 // false on the second or later calls. |
46 bool Init(const base::StringPiece& key) WARN_UNUSED_RESULT { | 51 bool Init(const base::StringPiece& key) WARN_UNUSED_RESULT { |
47 return Init(reinterpret_cast<const unsigned char*>(key.data()), | 52 return Init(reinterpret_cast<const unsigned char*>(key.data()), |
48 static_cast<int>(key.size())); | 53 static_cast<int>(key.size())); |
49 } | 54 } |
50 | 55 |
51 // Calculates the HMAC for the message in |data| using the algorithm supplied | 56 // Calculates the HMAC for the message in |data| using the algorithm supplied |
52 // to the constructor and the key supplied to the Init method. The HMAC is | 57 // to the constructor and the key supplied to the Init method. The HMAC is |
53 // returned in |digest|, which has |digest_length| bytes of storage available. | 58 // returned in |digest|, which has |digest_length| bytes of storage available. |
(...skipping 20 matching lines...) Expand all Loading... |
74 private: | 79 private: |
75 HashAlgorithm hash_alg_; | 80 HashAlgorithm hash_alg_; |
76 scoped_ptr<HMACPlatformData> plat_; | 81 scoped_ptr<HMACPlatformData> plat_; |
77 | 82 |
78 DISALLOW_COPY_AND_ASSIGN(HMAC); | 83 DISALLOW_COPY_AND_ASSIGN(HMAC); |
79 }; | 84 }; |
80 | 85 |
81 } // namespace crypto | 86 } // namespace crypto |
82 | 87 |
83 #endif // CRYPTO_HMAC_H_ | 88 #endif // CRYPTO_HMAC_H_ |
OLD | NEW |