Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
Add base::SHA1HashBytes.
Browse files Browse the repository at this point in the history
We need this for LevelDB.

BUG=64078
TEST=base_unittest --gtest_filter=SHA1Test

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77973 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hans@chromium.org committed Mar 13, 2011
1 parent 28cceee commit c139ef7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
5 changes: 5 additions & 0 deletions base/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ enum {
// hash.
std::string SHA1HashString(const std::string& str);

// Computes the SHA-1 hash of the |len| bytes in |data| and puts the hash
// in |hash|. |hash| must be SHA1_LENGTH bytes long.
void SHA1HashBytes(const unsigned char* data, size_t len,
unsigned char* hash);

} // namespace base

#endif // BASE_SHA1_H_
17 changes: 13 additions & 4 deletions base/sha1_portable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "base/sha1.h"

#include <string.h>

#include "base/basictypes.h"

namespace base {
Expand Down Expand Up @@ -195,12 +197,19 @@ void SecureHashAlgorithm::Process() {
}

std::string SHA1HashString(const std::string& str) {
char hash[SecureHashAlgorithm::kDigestSizeBytes];
SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()),
str.length(), reinterpret_cast<unsigned char*>(hash));
return std::string(hash, SecureHashAlgorithm::kDigestSizeBytes);
}

void SHA1HashBytes(const unsigned char* data, size_t len,
unsigned char* hash) {
SecureHashAlgorithm sha;
sha.Update(str.c_str(), str.length());
sha.Update(data, len);
sha.Final();
std::string out(reinterpret_cast<const char*>(sha.Digest()),
SecureHashAlgorithm::kDigestSizeBytes);
return out;

memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes);
}

} // namespace base
52 changes: 52 additions & 0 deletions base/sha1_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,55 @@ TEST(SHA1Test, Test3) {
for (size_t i = 0; i < base::SHA1_LENGTH; i++)
EXPECT_EQ(expected[i], output[i] & 0xFF);
}

TEST(SHA1Test, Test1Bytes) {
// Example A.1 from FIPS 180-2: one-block message.
std::string input = "abc";
unsigned char output[base::SHA1_LENGTH];

unsigned char expected[] = { 0xa9, 0x99, 0x3e, 0x36,
0x47, 0x06, 0x81, 0x6a,
0xba, 0x3e, 0x25, 0x71,
0x78, 0x50, 0xc2, 0x6c,
0x9c, 0xd0, 0xd8, 0x9d };

base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
input.length(), output);
for (size_t i = 0; i < base::SHA1_LENGTH; i++)
EXPECT_EQ(expected[i], output[i]);
}

TEST(SHA1Test, Test2Bytes) {
// Example A.2 from FIPS 180-2: multi-block message.
std::string input =
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
unsigned char output[base::SHA1_LENGTH];

unsigned char expected[] = { 0x84, 0x98, 0x3e, 0x44,
0x1c, 0x3b, 0xd2, 0x6e,
0xba, 0xae, 0x4a, 0xa1,
0xf9, 0x51, 0x29, 0xe5,
0xe5, 0x46, 0x70, 0xf1 };

base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
input.length(), output);
for (size_t i = 0; i < base::SHA1_LENGTH; i++)
EXPECT_EQ(expected[i], output[i]);
}

TEST(SHA1Test, Test3Bytes) {
// Example A.3 from FIPS 180-2: long message.
std::string input(1000000, 'a');
unsigned char output[base::SHA1_LENGTH];

unsigned char expected[] = { 0x34, 0xaa, 0x97, 0x3c,
0xd4, 0xc4, 0xda, 0xa4,
0xf6, 0x1e, 0xeb, 0x2b,
0xdb, 0xad, 0x27, 0x31,
0x65, 0x34, 0x01, 0x6f };

base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
input.length(), output);
for (size_t i = 0; i < base::SHA1_LENGTH; i++)
EXPECT_EQ(expected[i], output[i]);
}

0 comments on commit c139ef7

Please sign in to comment.