OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/hmac.h" |
| 6 |
| 7 #include <CommonCrypto/CommonHMAC.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace base { |
| 12 |
| 13 HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length) |
| 14 : hash_alg_(hash_alg), |
| 15 key_(reinterpret_cast<const char*>(key), key_length) { |
| 16 } |
| 17 |
| 18 HMAC::~HMAC() { |
| 19 // Zero out key copy. |
| 20 key_.assign(key_.length(), std::string::value_type()); |
| 21 key_.clear(); |
| 22 key_.reserve(0); |
| 23 } |
| 24 |
| 25 bool HMAC::Sign(const std::string& data, |
| 26 unsigned char* digest, |
| 27 int digest_length) { |
| 28 CCHmacAlgorithm algorithm; |
| 29 int algorithm_digest_length; |
| 30 switch (hash_alg_) { |
| 31 case SHA1: |
| 32 algorithm = kCCHmacAlgSHA1; |
| 33 algorithm_digest_length = CC_SHA1_DIGEST_LENGTH; |
| 34 break; |
| 35 default: |
| 36 NOTREACHED(); |
| 37 return false; |
| 38 } |
| 39 |
| 40 if (digest_length < algorithm_digest_length) { |
| 41 DCHECK(false); |
| 42 return false; |
| 43 } |
| 44 |
| 45 CCHmac(algorithm, |
| 46 key_.data(), key_.length(), data.data(), data.length(), |
| 47 digest); |
| 48 |
| 49 return true; |
| 50 } |
| 51 |
| 52 } // namespace base |
OLD | NEW |