| 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) |
| 18 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { | 18 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { |
| 19 // Only SHA-1 digest is supported now. |
| 20 DCHECK(hash_alg_ == SHA1); |
| 21 } |
| 22 |
| 23 bool HMAC::Init(const unsigned char *key, int key_length) { |
| 24 if (!plat_->key_.empty()) { |
| 25 // Init must not be called more than once on the same HMAC object. |
| 26 NOTREACHED(); |
| 27 return false; |
| 28 } |
| 29 |
| 19 plat_->key_.assign(reinterpret_cast<const char*>(key), key_length); | 30 plat_->key_.assign(reinterpret_cast<const char*>(key), key_length); |
| 31 |
| 32 return true; |
| 20 } | 33 } |
| 21 | 34 |
| 22 HMAC::~HMAC() { | 35 HMAC::~HMAC() { |
| 23 // Zero out key copy. | 36 // Zero out key copy. |
| 24 plat_->key_.assign(plat_->key_.length(), std::string::value_type()); | 37 plat_->key_.assign(plat_->key_.length(), std::string::value_type()); |
| 25 plat_->key_.clear(); | 38 plat_->key_.clear(); |
| 26 plat_->key_.reserve(0); | 39 plat_->key_.reserve(0); |
| 27 } | 40 } |
| 28 | 41 |
| 29 bool HMAC::Sign(const std::string& data, | 42 bool HMAC::Sign(const std::string& data, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 47 } | 60 } |
| 48 | 61 |
| 49 CCHmac(algorithm, | 62 CCHmac(algorithm, |
| 50 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(), | 63 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(), |
| 51 digest); | 64 digest); |
| 52 | 65 |
| 53 return true; | 66 return true; |
| 54 } | 67 } |
| 55 | 68 |
| 56 } // namespace base | 69 } // namespace base |
| OLD | NEW |