Skip to content

Commit

Permalink
Change HMAC::Sign() to take base::StringPiece instead of string.
Browse files Browse the repository at this point in the history
Do this to avoid memory copying when signning data in char*.
base::StringPiece nicely handles both cases.

BUG=None
TEST=crypto_unittests

Review URL: http://codereview.chromium.org/7033035

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88049 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hclam@chromium.org committed Jun 6, 2011
1 parent 083072f commit c28986e
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 9 deletions.
8 changes: 3 additions & 5 deletions crypto/hmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
#define CRYPTO_HMAC_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_piece.h"

namespace crypto {

Expand Down Expand Up @@ -42,7 +41,7 @@ class HMAC {

// Initializes this instance using |key|. Call Init only once. It returns
// false on the second or later calls.
bool Init(const std::string& key) {
bool Init(const base::StringPiece& key) {
return Init(reinterpret_cast<const unsigned char*>(key.data()),
static_cast<int>(key.size()));
}
Expand All @@ -51,8 +50,7 @@ class HMAC {
// to the constructor and the key supplied to the Init method. The HMAC is
// returned in |digest|, which has |digest_length| bytes of storage available.
// TODO(abarth): digest_length should be a size_t.
bool Sign(const std::string& data,
unsigned char* digest,
bool Sign(const base::StringPiece& data, unsigned char* digest,
int digest_length) const;

// TODO(albertb): Add a Verify method.
Expand Down
2 changes: 1 addition & 1 deletion crypto/hmac_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ HMAC::~HMAC() {
plat_->key_.reserve(0);
}

bool HMAC::Sign(const std::string& data,
bool HMAC::Sign(const base::StringPiece& data,
unsigned char* digest,
int digest_length) const {
CCHmacAlgorithm algorithm;
Expand Down
2 changes: 1 addition & 1 deletion crypto/hmac_nss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool HMAC::Init(const unsigned char *key, int key_length) {
return true;
}

bool HMAC::Sign(const std::string& data,
bool HMAC::Sign(const base::StringPiece& data,
unsigned char* digest,
int digest_length) const {
if (!plat_->sym_key_.get()) {
Expand Down
2 changes: 1 addition & 1 deletion crypto/hmac_openssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ HMAC::~HMAC() {
STLClearObject(&plat_->key);
}

bool HMAC::Sign(const std::string& data,
bool HMAC::Sign(const base::StringPiece& data,
unsigned char* digest,
int digest_length) const {
DCHECK_GE(digest_length, 0);
Expand Down
2 changes: 1 addition & 1 deletion crypto/hmac_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ bool HMAC::Init(const unsigned char* key, int key_length) {
HMAC::~HMAC() {
}

bool HMAC::Sign(const std::string& data,
bool HMAC::Sign(const base::StringPiece& data,
unsigned char* digest,
int digest_length) const {
if (hash_alg_ == SHA256) {
Expand Down

0 comments on commit c28986e

Please sign in to comment.