This source file includes following definitions.
- plat_
- Init
- Sign
#include "crypto/hmac.h"
#include <openssl/hmac.h>
#include <algorithm>
#include <vector>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "crypto/openssl_util.h"
namespace crypto {
struct HMACPlatformData {
std::vector<unsigned char> key;
};
HMAC::HMAC(HashAlgorithm hash_alg)
: hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256);
}
bool HMAC::Init(const unsigned char* key, size_t key_length) {
DCHECK(plat_->key.empty());
plat_->key.assign(key, key + key_length);
return true;
}
HMAC::~HMAC() {
plat_->key.assign(plat_->key.size(), 0);
STLClearObject(&plat_->key);
}
bool HMAC::Sign(const base::StringPiece& data,
unsigned char* digest,
size_t digest_length) const {
DCHECK(!plat_->key.empty());
ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length);
return ::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(),
&plat_->key[0], plat_->key.size(),
reinterpret_cast<const unsigned char*>(data.data()),
data.size(),
result.safe_buffer(), NULL);
}
}