blob: 542afa433356fcfce009b6f6f42e1f1a32157a3d [file] [edit]
// Copyright 2026 Google LLC
//
// 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
//
// https://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.
//
#include "crypto/default/hasher.h"
#include <string>
#include <utility>
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/strings/escaping.h"
#include "absl/strings/string_view.h"
#include "crypto/algorithms.h"
#include "crypto/hash.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace credentio {
namespace {
using ::absl_testing::StatusIs;
using ::testing::Eq;
using ::testing::IsFalse;
using ::testing::IsTrue;
using ::testing::NotNull;
std::string BytesFromHexOrDie(absl::string_view hex) {
std::string bytes;
CHECK(absl::HexStringToBytes(hex, &bytes));
return bytes;
}
TEST(CreateHasherTest, SHA256) {
auto hasher_or = CreateHasher("sha256");
ASSERT_TRUE(hasher_or.ok());
auto hasher = std::move(hasher_or).value();
hasher->Update(absl::string_view("hello"));
hasher->Update(absl::string_view("world"));
EXPECT_THAT(
absl::BytesToHexString(hasher->Digest()),
"936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af");
}
TEST(CreateHasherTest, SHA384) {
auto hasher_or = CreateHasher("sha384");
ASSERT_TRUE(hasher_or.ok());
auto hasher = std::move(hasher_or).value();
hasher->Update(absl::string_view("hello"));
hasher->Update(absl::string_view("world"));
EXPECT_THAT(absl::BytesToHexString(hasher->Digest()),
"97982a5b1414b9078103a1c008c4e3526c27b41cdbcf80790560a40f2a9bf2ed"
"4427ab1428789915ed4b3dc07c454bd9");
}
TEST(CreateHasherTest, SHA512) {
auto hasher_or = CreateHasher("sha512");
ASSERT_TRUE(hasher_or.ok());
auto hasher = std::move(hasher_or).value();
hasher->Update(absl::string_view("hello"));
hasher->Update(absl::string_view("world"));
EXPECT_THAT(
absl::BytesToHexString(hasher->Digest()),
"1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa"
"98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60");
}
TEST(CreateHasherTest, UnknownAlgorithm) {
EXPECT_THAT(CreateHasher("blah"),
StatusIs(absl::StatusCode::kInvalidArgument,
"unrecognized hash algorithm name"));
}
TEST(HashCheckerTest, Valid) {
auto checker_or = DefaultHashCheckerFactory().Create(HashAlgorithm::kSha256);
ASSERT_TRUE(checker_or.ok());
auto checker = std::move(checker_or).value();
ASSERT_THAT(checker, NotNull());
checker->Update("hello");
checker->Update("world");
EXPECT_THAT(
checker->Check(BytesFromHexOrDie(
"936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af")),
IsTrue());
}
TEST(HashCheckerTest, Invalid) {
auto checker_or = DefaultHashCheckerFactory().Create(HashAlgorithm::kSha256);
ASSERT_TRUE(checker_or.ok());
auto checker = std::move(checker_or).value();
ASSERT_THAT(checker, NotNull());
checker->Update("hello");
checker->Update("world2");
EXPECT_THAT(
checker->Check(BytesFromHexOrDie(
"936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af")),
IsFalse());
}
TEST(CreateHasherTest, SHA256HashFactory) {
Sha256HasherFactory factory;
EXPECT_EQ(factory.algorithm(), HashAlgorithm::kSha256);
auto hasher_or = factory.Create();
ASSERT_TRUE(hasher_or.ok());
auto hasher = std::move(hasher_or).value();
hasher->Update(absl::string_view("hello"));
hasher->Update(absl::string_view("world"));
EXPECT_THAT(
absl::BytesToHexString(hasher->Digest()),
"936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af");
}
TEST(CreateHasherTest, SHA384HashFactory) {
Sha384HasherFactory factory;
EXPECT_EQ(factory.algorithm(), HashAlgorithm::kSha384);
auto hasher_or = factory.Create();
ASSERT_TRUE(hasher_or.ok());
auto hasher = std::move(hasher_or).value();
hasher->Update(absl::string_view("hello"));
hasher->Update(absl::string_view("world"));
EXPECT_THAT(absl::BytesToHexString(hasher->Digest()),
"97982a5b1414b9078103a1c008c4e3526c27b41cdbcf80790560a40f2a9bf2ed"
"4427ab1428789915ed4b3dc07c454bd9");
}
TEST(CreateHasherTest, SHA512HashFactory) {
Sha512HasherFactory factory;
EXPECT_EQ(factory.algorithm(), HashAlgorithm::kSha512);
auto hasher_or = factory.Create();
ASSERT_TRUE(hasher_or.ok());
auto hasher = std::move(hasher_or).value();
hasher->Update(absl::string_view("hello"));
hasher->Update(absl::string_view("world"));
EXPECT_THAT(
absl::BytesToHexString(hasher->Digest()),
"1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa"
"98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60");
}
TEST(HasherFactoryTest, DefaultIsSha256) {
const HasherFactory& factory = DefaultHasherFactory();
EXPECT_THAT(factory.algorithm(), Eq(HashAlgorithm::kSha256));
}
TEST(HasherFactoryTest, DefaultIsSingleton) {
const HasherFactory& factory1 = DefaultHasherFactory();
const HasherFactory& factory2 = DefaultHasherFactory();
EXPECT_THAT(&factory1, Eq(&factory2));
}
} // namespace
} // namespace credentio