diff options
author | dominich@chromium.org <dominich@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-26 23:39:50 +0000 |
---|---|---|
committer | dominich@chromium.org <dominich@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-26 23:39:50 +0000 |
commit | a3bcff633580fa9afd4653817f497d8a49235994 (patch) | |
tree | d81bbfab47ed019ea0eb47ccdfb914944f7bafdf /base | |
parent | d045e3746c58aa12b603dc596e683f4f9f14f5a9 (diff) | |
download | chromium_src-a3bcff633580fa9afd4653817f497d8a49235994.zip chromium_src-a3bcff633580fa9afd4653817f497d8a49235994.tar.gz chromium_src-a3bcff633580fa9afd4653817f497d8a49235994.tar.bz2 |
MD5Update function uses StringPiece instead of raw buffer.
BUG=none
TEST=base_unittests --gtest_filter=MD5.ContextWithStringData*
Review URL: http://codereview.chromium.org/7466003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@94203 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/md5.cc | 14 | ||||
-rw-r--r-- | base/md5.h | 13 | ||||
-rw-r--r-- | base/md5_unittest.cc | 18 |
3 files changed, 33 insertions, 12 deletions
diff --git a/base/md5.cc b/base/md5.cc index 2211a28..754994c 100644 --- a/base/md5.cc +++ b/base/md5.cc @@ -1,5 +1,8 @@ +// Copyright (c) 2011 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. + // The original file was copied from sqlite, and was in the public domain. -// Modifications Copyright 2006 Google Inc. All Rights Reserved /* * This code implements the MD5 message-digest algorithm. @@ -164,7 +167,9 @@ void MD5Init(MD5Context* context) { * Update context to reflect the concatenation of another buffer full * of bytes. */ -void MD5Update(MD5Context* context, const void* inbuf, size_t len) { +void MD5Update(MD5Context* context, const StringPiece& data) { + const unsigned char* inbuf = (const unsigned char*)data.data(); + size_t len = data.size(); struct Context *ctx = (struct Context *)context; const unsigned char* buf = (const unsigned char*)inbuf; uint32 t; @@ -273,11 +278,12 @@ std::string MD5DigestToBase16(const MD5Digest& digest) { void MD5Sum(const void* data, size_t length, MD5Digest* digest) { MD5Context ctx; MD5Init(&ctx); - MD5Update(&ctx, static_cast<const unsigned char*>(data), length); + MD5Update(&ctx, + StringPiece(reinterpret_cast<const char*>(data), length)); MD5Final(digest, &ctx); } -std::string MD5String(const std::string& str) { +std::string MD5String(const StringPiece& str) { MD5Digest digest; MD5Sum(str.data(), str.length(), &digest); return MD5DigestToBase16(digest); @@ -6,9 +6,8 @@ #define BASE_MD5_H_ #pragma once -#include <string> - #include "base/base_api.h" +#include "base/string_piece.h" namespace base { @@ -52,10 +51,10 @@ BASE_API void MD5Sum(const void* data, size_t length, MD5Digest* digest); // MD5Update(). BASE_API void MD5Init(MD5Context* context); -// For the given buffer of data, updates the given MD5 context with the sum of -// the data. You can call this any number of times during the computation, -// except that MD5Init() must have been called first. -BASE_API void MD5Update(MD5Context* context, const void* data, size_t length); +// For the given buffer of |data| as a StringPiece, updates the given MD5 +// context with the sum of the data. You can call this any number of times +// during the computation, except that MD5Init() must have been called first. +BASE_API void MD5Update(MD5Context* context, const StringPiece& data); // Finalizes the MD5 operation and fills the buffer with the digest. BASE_API void MD5Final(MD5Digest* digest, MD5Context* context); @@ -64,7 +63,7 @@ BASE_API void MD5Final(MD5Digest* digest, MD5Context* context); BASE_API std::string MD5DigestToBase16(const MD5Digest& digest); // Returns the MD5 (in hexadecimal) of a string. -BASE_API std::string MD5String(const std::string& str); +BASE_API std::string MD5String(const StringPiece& str); } // namespace base diff --git a/base/md5_unittest.cc b/base/md5_unittest.cc index c81ff12..a20d819 100644 --- a/base/md5_unittest.cc +++ b/base/md5_unittest.cc @@ -120,7 +120,8 @@ TEST(MD5, ContextWithLongData) { if (len > length - total) len = length - total; - MD5Update(&ctx, data.get() + total, len); + MD5Update(&ctx, + StringPiece(reinterpret_cast<char*>(data.get() + total), len)); total += len; } @@ -188,4 +189,19 @@ TEST(MD5, MD5StringTestSuite7) { EXPECT_EQ(expected, actual); } +TEST(MD5, ContextWithStringData) { + MD5Context ctx; + MD5Init(&ctx); + + MD5Update(&ctx, "abc"); + + MD5Digest digest; + MD5Final(&digest, &ctx); + + std::string actual = MD5DigestToBase16(digest); + std::string expected = "900150983cd24fb0d6963f7d28e17f72"; + + EXPECT_EQ(expected, actual); +} + } // namespace base |