OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 #include "base/hmac.h" | 5 #include "base/hmac.h" |
6 | 6 |
7 #include <CommonCrypto/CommonHMAC.h> | 7 #include <CommonCrypto/CommonHMAC.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 struct HMACPlatformData { | 13 struct HMACPlatformData { |
14 std::string key_; | 14 std::string key_; |
15 }; | 15 }; |
16 | 16 |
17 HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length) | 17 HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length) |
18 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { | 18 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { |
19 plat_->key_.assign(reinterpret_cast<const char*>(key), key_length); | 19 plat_->key_.assign(reinterpret_cast<const char*>(key), key_length); |
20 } | 20 } |
21 | 21 |
22 HMAC::~HMAC() { | 22 HMAC::~HMAC() { |
23 // Zero out key copy. | 23 // Zero out key copy. |
24 plat_->key_.assign(plat_->key_.length(), std::string::value_type()); | 24 plat_->key_.assign(plat_->key_.length(), std::string::value_type()); |
25 plat_->key_.clear(); | 25 plat_->key_.clear(); |
26 plat_->key_.reserve(0); | 26 plat_->key_.reserve(0); |
27 | |
28 delete plat_; | |
29 } | 27 } |
30 | 28 |
31 bool HMAC::Sign(const std::string& data, | 29 bool HMAC::Sign(const std::string& data, |
32 unsigned char* digest, | 30 unsigned char* digest, |
33 int digest_length) { | 31 int digest_length) { |
34 CCHmacAlgorithm algorithm; | 32 CCHmacAlgorithm algorithm; |
35 int algorithm_digest_length; | 33 int algorithm_digest_length; |
36 switch (hash_alg_) { | 34 switch (hash_alg_) { |
37 case SHA1: | 35 case SHA1: |
38 algorithm = kCCHmacAlgSHA1; | 36 algorithm = kCCHmacAlgSHA1; |
(...skipping 10 matching lines...) Expand all Loading... |
49 } | 47 } |
50 | 48 |
51 CCHmac(algorithm, | 49 CCHmac(algorithm, |
52 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(), | 50 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(), |
53 digest); | 51 digest); |
54 | 52 |
55 return true; | 53 return true; |
56 } | 54 } |
57 | 55 |
58 } // namespace base | 56 } // namespace base |
OLD | NEW |