From 3d808c6f8099b19cd85d68e83e4c670caa0a02f3 Mon Sep 17 00:00:00 2001 From: hai007 Date: Fri, 9 Apr 2021 14:42:20 -0700 Subject: [PATCH] Internal change PiperOrigin-RevId: 367707033 --- cpp/platform/impl/windows/BUILD | 41 ++++++++++++++++++ cpp/platform/impl/windows/crypto.cc | 54 ++++++++++++++++++++++++ cpp/platform/impl/windows/crypto_test.cc | 50 ++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 cpp/platform/impl/windows/BUILD create mode 100644 cpp/platform/impl/windows/crypto.cc create mode 100644 cpp/platform/impl/windows/crypto_test.cc diff --git a/cpp/platform/impl/windows/BUILD b/cpp/platform/impl/windows/BUILD new file mode 100644 index 00000000..fe2dcdfa --- /dev/null +++ b/cpp/platform/impl/windows/BUILD @@ -0,0 +1,41 @@ +# Copyright 2020 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. + +cc_library( + name = "crypto", + testonly = True, + srcs = [ + "crypto.cc", + ], + visibility = ["//visibility:private"], + deps = [ + "//platform/api:types", + "//platform/base", + "//absl/strings", + "//openssl:crypto", + ], +) + +cc_test( + name = "impl_test", + size = "small", + srcs = [ + "crypto_test.cc", + ], + deps = [ + ":crypto", + "//platform/api:types", + "//testing/base/public:gunit_main", + ], +) diff --git a/cpp/platform/impl/windows/crypto.cc b/cpp/platform/impl/windows/crypto.cc new file mode 100644 index 00000000..263bec27 --- /dev/null +++ b/cpp/platform/impl/windows/crypto.cc @@ -0,0 +1,54 @@ +// Copyright 2020 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 "platform/api/crypto.h" + +#include +#include + +#include "platform/base/byte_array.h" +#include "absl/strings/string_view.h" +#include "openssl/digest.h" + +// Function implementations for platform/api/crypto.h. +namespace location { +namespace nearby { + +// Initialize global crypto state. +void Crypto::Init() {} + +static ByteArray Hash(absl::string_view input, const EVP_MD* algo) { + unsigned int md_out_size = EVP_MAX_MD_SIZE; + uint8_t digest_buffer[EVP_MAX_MD_SIZE]; + if (input.empty()) return {}; + + if (!EVP_Digest(input.data(), input.size(), digest_buffer, &md_out_size, algo, + nullptr)) + return {}; + + return ByteArray{reinterpret_cast(digest_buffer), md_out_size}; +} + +// Return MD5 hash of input. +ByteArray Crypto::Md5(absl::string_view input) { + return Hash(input, EVP_md5()); +} + +// Return SHA256 hash of input. +ByteArray Crypto::Sha256(absl::string_view input) { + return Hash(input, EVP_sha256()); +} + +} // namespace nearby +} // namespace location diff --git a/cpp/platform/impl/windows/crypto_test.cc b/cpp/platform/impl/windows/crypto_test.cc new file mode 100644 index 00000000..5276a9e6 --- /dev/null +++ b/cpp/platform/impl/windows/crypto_test.cc @@ -0,0 +1,50 @@ +// Copyright 2020 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 "platform/api/crypto.h" + +#include "gtest/gtest.h" + +namespace location { +namespace nearby { +namespace { + +TEST(CryptoTest, Md5Hash) { + const std::string input{"Hello Nearby Connection"}; + const ByteArray expected_md5( + "\x94\xa3\xbe\xc1\x8d\x30\xe3\x24\x5f\xa1\x4c\xee\xe7\x52\xe9\x36"); + ByteArray md5_hash = Crypto::Md5(input); + EXPECT_EQ(md5_hash, expected_md5); +} + +TEST(CryptoTest, Md5HashOnEmptyInput) { + EXPECT_EQ(Crypto::Md5(""), ByteArray{}); +} + +TEST(CryptoTest, Sha256Hash) { + const std::string input("Hello Nearby Connection"); + const ByteArray expected_sha256( + "\xb4\x24\xd3\xc0\x58\x12\x9a\x42\xcb\x81\xa0\x4b\x6e\x9d\xfe\x45\x45\x9f" + "\x15\xf7\xc0\xa9\x32\x2f\xfb\x9\x45\xf0\xf9\xbe\x75\xb"); + ByteArray sha256_hash = Crypto::Sha256(input); + EXPECT_EQ(sha256_hash, expected_sha256); +} + +TEST(CryptoTest, Sha256HashOnEmptyInput) { + EXPECT_EQ(Crypto::Sha256(""), ByteArray{}); +} + +} // namespace +} // namespace nearby +} // namespace location