Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/istio/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ cc_library(
name = "headers_lib",
hdrs = [
"attributes_builder.h",
"concat_hash.h",
"local_attributes.h",
"md5.h",
"protobuf.h",
"status.h",
],
Expand Down
77 changes: 77 additions & 0 deletions include/istio/utils/concat_hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Copyright 2017 Istio Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ISTIO_UTILS_CONCAT_HASH_H_
#define ISTIO_UTILS_CONCAT_HASH_H_

#include <string.h>
#include <string>

namespace istio {
namespace utils {

// This class concatenates multiple values into a string as hash
class ConcatHash {
public:
ConcatHash(size_t reserve_size) { hash_.reserve(reserve_size); }

// Updates the context with data.
ConcatHash& Update(const void* data, size_t size) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can consolidate 3 of Update with string_view.

hash_.append(static_cast<const char*>(data), size);
return *this;
}

// A helper function for int
ConcatHash& Update(int d) { return Update(&d, sizeof(d)); }

// A helper function for const char*
ConcatHash& Update(const char* str) {
hash_.append(str);
return *this;
}

// A helper function for const string
ConcatHash& Update(const std::string& str) {
hash_.append(str);
return *this;
}

// Returns the concated string as hash.
std::string getHash() const { return hash_; }

// Converts a binary string to a printable string for unit-test only
static std::string DebugString(const std::string& hash) {
std::string out;
out.reserve(hash.size() * 2);
for (auto c : hash) {
if (std::isprint(c)) {
out.append(1, c);
} else {
char buf[10];
sprintf(buf, "%02x", (unsigned char)c);
out.append(buf);
}
}
return out;
}

private:
std::string hash_;
};

} // namespace utils
} // namespace istio

#endif // ISTIO_UTILS_CONCAT_HASH_H_
68 changes: 0 additions & 68 deletions include/istio/utils/md5.h

This file was deleted.

1 change: 0 additions & 1 deletion src/istio/mixerclient/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ cc_library(
"//include/istio/quota_config:requirement_header",
"//include/istio/utils:simple_lru_cache",
"//src/istio/prefetch:quota_prefetch_lib",
"//src/istio/utils:md5_lib",
"//src/istio/utils:utils_lib",
],
)
Expand Down
11 changes: 6 additions & 5 deletions src/istio/mixerclient/referenced.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace mixerclient {
namespace {
const char kDelimiter[] = "\0";
const int kDelimiterLength = 1;
const size_t kMaxConcatHashSize = 4096;
const std::string kWordDelimiter = ":";

// Decode dereferences index into str using global and local word lists.
Expand Down Expand Up @@ -63,7 +64,7 @@ bool Decode(int idx, const std::vector<std::string> &global_words,

// Updates hasher with keys
void Referenced::UpdateHash(const std::vector<AttributeRef> &keys,
utils::MD5 *hasher) {
utils::ConcatHash *hasher) {
// keys are already sorted during Fill
for (const AttributeRef &key : keys) {
hasher->Update(key.name);
Expand Down Expand Up @@ -203,7 +204,7 @@ void Referenced::CalculateSignature(const Attributes &attributes,
std::string *signature) const {
const auto &attributes_map = attributes.attributes();

utils::MD5 hasher;
utils::ConcatHash hasher(kMaxConcatHashSize);
for (std::size_t i = 0; i < exact_keys_.size(); ++i) {
const auto &key = exact_keys_[i];
const auto it = attributes_map.find(key.name);
Expand Down Expand Up @@ -274,18 +275,18 @@ void Referenced::CalculateSignature(const Attributes &attributes,
}
hasher.Update(extra_key);

*signature = hasher.Digest();
*signature = hasher.getHash();
}

std::string Referenced::Hash() const {
utils::MD5 hasher;
utils::ConcatHash hasher(kMaxConcatHashSize);

// keys are sorted during Fill
UpdateHash(absence_keys_, &hasher);
hasher.Update(kWordDelimiter);
UpdateHash(exact_keys_, &hasher);

return hasher.Digest();
return hasher.getHash();
}

std::string Referenced::DebugString() const {
Expand Down
4 changes: 2 additions & 2 deletions src/istio/mixerclient/referenced.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <vector>

#include "include/istio/utils/md5.h"
#include "include/istio/utils/concat_hash.h"
#include "mixer/v1/check.pb.h"

namespace istio {
Expand Down Expand Up @@ -86,7 +86,7 @@ class Referenced {

// Updates hasher with keys
static void UpdateHash(const std::vector<AttributeRef> &keys,
utils::MD5 *hasher);
utils::ConcatHash *hasher);
};

} // namespace mixerclient
Expand Down
22 changes: 15 additions & 7 deletions src/istio/mixerclient/referenced_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "src/istio/mixerclient/referenced.h"

#include "include/istio/utils/attributes_builder.h"
#include "include/istio/utils/md5.h"
#include "include/istio/utils/concat_hash.h"

#include "google/protobuf/text_format.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -177,8 +177,10 @@ TEST(ReferencedTest, FillSuccessTest) {
"duration-key, int-key, string-key, string-map-key[If-Match], "
"time-key, ");

EXPECT_EQ(utils::MD5::DebugString(referenced.Hash()),
"602d5bbd45b623c3560d2bdb6104f3ab");
EXPECT_EQ(utils::ConcatHash::DebugString(referenced.Hash()),
"string-map-key00User-Agent00target.name00target.service00:bool-"
"key00bytes-key00double-key00duration-key00int-key00string-"
"key00string-map-key00If-Match00time-key00");
}

TEST(ReferencedTest, FillFail1Test) {
Expand Down Expand Up @@ -249,8 +251,13 @@ TEST(ReferencedTest, OKSignature1Test) {
std::string signature;
EXPECT_TRUE(referenced.Signature(attributes, "extra", &signature));

EXPECT_EQ(utils::MD5::DebugString(signature),
"751b028b2e2c230ef9c4e59ac556ca04");
EXPECT_EQ(utils::ConcatHash::DebugString(signature),
"bool-key000100bytes-key00this is a bytes "
"value00double-key009a99999999f9X@00duration-"
"key000500000000000000000000000000int-key00#0000000000000000string-"
"key00this is a string "
"value00string-map-key00If-Match00value10000time-"
"key000000000000000000000000000000extra");
}

TEST(ReferencedTest, StringMapReferencedTest) {
Expand All @@ -271,8 +278,9 @@ TEST(ReferencedTest, StringMapReferencedTest) {

std::string signature;
EXPECT_TRUE(referenced.Signature(attrs, "extra", &signature));
EXPECT_EQ(utils::MD5::DebugString(signature),
"bc055468af1a0d4d03ec7f6fa2265b9b");
EXPECT_EQ(utils::ConcatHash::DebugString(signature),
"map-key100value100map-key200exact-subkey400subvalue400exact-"
"subkey500subvalue50000extra");

// negative test: map-key3 must absence
::istio::mixer::v1::Attributes attr1(attrs);
Expand Down
25 changes: 0 additions & 25 deletions src/istio/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ cc_test(
],
)

cc_library(
name = "md5_lib",
srcs = ["md5.cc"],
visibility = ["//visibility:public"],
deps = [
"//external:boringssl_crypto",
"//include/istio/utils:headers_lib",
],
)

cc_test(
name = "simple_lru_cache_test",
size = "small",
Expand All @@ -70,21 +60,6 @@ cc_test(
],
)

cc_test(
name = "md5_test",
size = "small",
srcs = ["md5_test.cc"],
linkopts = [
"-lm",
"-lpthread",
],
linkstatic = 1,
deps = [
":md5_lib",
"//external:googletest_main",
],
)

cc_library(
name = "attribute_names_lib",
srcs = [
Expand Down
55 changes: 0 additions & 55 deletions src/istio/utils/md5.cc

This file was deleted.

Loading