blob: 668b3066271074a4cd96b1b045c332c430912921 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/hmac.h"
#include <CommonCrypto/CommonHMAC.h>
#include "base/logging.h"
namespace base {
struct HMACPlatformData {
std::string key_;
};
HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length)
: hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
plat_->key_.assign(reinterpret_cast<const char*>(key), key_length);
}
HMAC::~HMAC() {
// Zero out key copy.
plat_->key_.assign(plat_->key_.length(), std::string::value_type());
plat_->key_.clear();
plat_->key_.reserve(0);
}
bool HMAC::Sign(const std::string& data,
unsigned char* digest,
int digest_length) {
CCHmacAlgorithm algorithm;
int algorithm_digest_length;
switch (hash_alg_) {
case SHA1:
algorithm = kCCHmacAlgSHA1;
algorithm_digest_length = CC_SHA1_DIGEST_LENGTH;
break;
default:
NOTREACHED();
return false;
}
if (digest_length < algorithm_digest_length) {
NOTREACHED();
return false;
}
CCHmac(algorithm,
plat_->key_.data(), plat_->key_.length(), data.data(), data.length(),
digest);
return true;
}
} // namespace base
|